g1smd

msg:4501936 | 7:47 am on Sep 30, 2012 (gmt 0) |
Use example.com in this forum to suppress URL auto-linking. It creates very little server load. Link to href="www.example.com/book/name/motor_car" from the pages of your site and add a RewriteRule to match those requests to the actual internal filepath that will serve the content. As this is a question that has been asked several times per week for the last decade here, there are several thousand prior threads with example code. Use these for ideas, test your code, then post here about any specific problems you are having. Do use extensionless URLs. Avoid all underscores and spaces in URLs. Strip out any "special characters" as encoded URLs are not easy to read.
|
HaloPlayer

msg:4501954 | 10:42 am on Sep 30, 2012 (gmt 0) |
Thanks g1smd, I will have another search on the forums, I understand it must be a common question.
|
HaloPlayer

msg:4501963 | 11:23 am on Sep 30, 2012 (gmt 0) |
Something like this will work for me do you think?:
RewriteEngine on RewriteRule ^book/([a-z]+/([a-z]+)$ book.php?$1=$2 [L] Also do you think it is better to leave it without an extension or will adding a .htm or html for each URL be better for search engines?
|
g1smd

msg:4501967 | 11:42 am on Sep 30, 2012 (gmt 0) |
Use extensionless URLs. The rule looks good. Do you need [0-9] and [-] in addition to [a-z] here? Do ensure all URLs are lower case. Once you have a site with a lot of rewriting you'll appreciate the simplicity that comes with the scheme "extensionless URL requests will be rewritten and URL requests with extensions are for real files such as images, CSS or JS".
|
HaloPlayer

msg:4501979 | 12:26 pm on Sep 30, 2012 (gmt 0) |
Theres a simpler rewrite? I also checked that Apache has mod_rewrite module enabled
|
HaloPlayer

msg:4502922 | 11:52 am on Oct 2, 2012 (gmt 0) |
I found this handy rewrite which will remove the .php extension:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php I can't seem to find anything to do this though: www.example.com/book.php?name=motor_car Turn into: www.example.com/book/name/motor_car Any help would be appreciated. [edited by: incrediBILL at 9:35 pm (utc) on Oct 7, 2012] [edit reason] fixed URLS, use Example.com [/edit]
|
g1smd

msg:4502929 | 12:18 pm on Oct 2, 2012 (gmt 0) |
That code doesn't "remove the PHP extension". It maps extensionless URL requests to an internal .php file to serve the content, using an internal rewrite. What happened to the code in post #4501963? I thought that was the rewrite you needed? There's another problem. If someone were to request the URL and include .php on the end, they would still reach the content, and would be doing so at a Duplicate URL. To fix that problem you need an additional rule, one that redirects the user to make a new request for the extensionless URL. Be aware that htaccess does not "make" URLs. If you want a user to "see" and "use" a particular URL, then you must put that URL in the href="" link on the page for them to click on. htaccess kicks in after the link is clicked. htaccess processes the request that the browser makes.
|
HaloPlayer

msg:4502934 | 12:58 pm on Oct 2, 2012 (gmt 0) |
Thanks for that. I did another extensive search with various terms and had to go back to 2011, this thread should do the trick I will try the method tomorrow: [webmasterworld.com...]
|
g1smd

msg:4502947 | 1:15 pm on Oct 2, 2012 (gmt 0) |
Yes, that post contains most of what you need. This topic comes up every week so there are a LOT of previous examples to look at. :)
|
HaloPlayer

msg:4503417 | 11:08 am on Oct 3, 2012 (gmt 0) |
No worries :-) You should make a sticky thread with a few of the examples of a rewrite...just a thought
|
Andy Langton

msg:4503420 | 11:33 am on Oct 3, 2012 (gmt 0) |
Don't forget the Apache forum library [webmasterworld.com], including Cruft Free URLs for Dummies [webmasterworld.com]
|
Leosghost

msg:4503421 | 11:33 am on Oct 3, 2012 (gmt 0) |
| You should make a sticky thread with a few of the examples of a rewrite...just a thought |
| Library..there is one per forum.. [webmasterworld.com...]
|
HaloPlayer

msg:4503423 | 11:44 am on Oct 3, 2012 (gmt 0) |
Thank you :-)
|
HaloPlayer

msg:4505045 | 11:12 am on Oct 7, 2012 (gmt 0) |
Oddly enough I have tried the rewrite and it just will not work for me, I've tried variations of it, I spent a good 2.5 hours fiddling around with it, found several other sites relating too it. This is the code I am using:
Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^book/([0-9]+) /book.php?name=$1 [L] The mod rewrite module is definetely installed on the server. If I use this rewrite:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php This will work fine in hiding the .php extension so I know these rewrites work on the server. Hiding the .php extension is what I am after but I am also after hiding the book?name= to make it /book/ What am I doing wrong? [edited by: incrediBILL at 9:38 pm (utc) on Oct 7, 2012] [edit reason] removed URLS, see TOS & Charter [/edit]
|
g1smd

msg:4505058 | 11:47 am on Oct 7, 2012 (gmt 0) |
Again, this code does NOT "hide the php extension". You have this exactly backwards. It maps extensionless URL requests to an internal .php file to serve the content, using an internal rewrite. In your non-working code, what is this line for?
RewriteCond %{REQUEST_FILENAME}\.php -f It has this effect: For the request example.com/book/78456, only if there's a physical file on the server called 78456.php then rewrite the original request to fetch content from /book.php/name=78456
|
HaloPlayer

msg:4505069 | 1:32 pm on Oct 7, 2012 (gmt 0) |
g1smd thanks for your efforts, I got it to work, the problem was in relation to the code:
^book/([0-9]+) I had the numerical value (0-9) and should have had alphabetical (A-Z) and you did mention this in your second post, I apologise for not paying attention this this, I did not realise how picky the code can be, I should have asked what that section of code does. I have added alphabetical and numerical values as well as underscore. I recall you saying avoid underscores, I would be better with dashes (-) is this purely because of the way the code handles underscores or does it also have a SEO value? So others who wan't to do similar, here is my final code:
Options +FollowSymlinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^book/([-a-zA-Z0-9_]+) /book.php?name=$1 [L] Thank you again for your efforts g1smd. PS what does the [L] mean at the end of the RewriteRule? And also is the Options +FollowSymlinks actually required?
|
g1smd

msg:4505071 | 1:37 pm on Oct 7, 2012 (gmt 0) |
The Options line may be optional or it may be required. It depends on how your host set up your server. Sometimes you must remove it for anything to work. The [L] makes this the Last rule if THIS rule matched. Not using underscore has SEO value. Also underscore "disappears" in underlined links. Avoid it. In your character group, the hyphen should be listed last. Are you really using mixed cased URLs? That will be a problem. If you insist in doing so, replace a-zA-Z with a-z and the [NC] flag. It would be far better to use all lower case URLs.
|
HaloPlayer

msg:4505084 | 2:03 pm on Oct 7, 2012 (gmt 0) |
They disappear in underlined links, what does this mean? No I'm not using mixed case URL's, I just thought adding it could avoid any problems just in case they were needed, I will just remove that. All my links are lower case. I will have to put an exlude in their as well I just realised as CSS and images aren't showing, would this be a good rule:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png|\.css)$
|
g1smd

msg:4505088 | 3:14 pm on Oct 7, 2012 (gmt 0) |
You could do it that way, but why having found the period once would you search for it again? It simplifies to
!\.(gif|jpe?g|png|css)$ but do take a look at the original pattern again:
^book/([a-z0-9-]+) It says "begins" with "book", then "slash" then "characters or digits", but does not say how it ENDS. It "allows" requests with period and extension without capturing them. It should disallow extensions, so think about end anchoring that pattern rather than adding another condition. Make sure that all links to images and CSS files BEGIN with a leading slash, and mention the full path to the file. Yes, do disallow upper case URLs in the rewrite process. To allow both cases for any page promotes duplicate content. Do consider adding a separate ruleset for redirecting upper case requests to lower case. That's a regular question here, so there's plenty of example code.
|
incrediBILL

msg:4505198 | 9:46 pm on Oct 7, 2012 (gmt 0) |
| They disappear in underlined links, what does this mean? |
| That underlines overlap underlined links, people can't tell there's a character there. Here's an example of what we mean: [underlines_in_underlined_url.example.com...] Are the above words separated by spaces or with underlines between the words? How can you easily tell as the whole thing in underlined? It's user unfriendly. Here's an example hyphens instead: [hyphens-in-underlined-url.example.com...] When hyphenated and underlined you can still tell it's hyphenated.
|
HaloPlayer

msg:4506415 | 11:56 am on Oct 10, 2012 (gmt 0) |
g1smd when you say anchoring that pattern rather than adding a new condition are you meaning completely disallowing extensions in the request? The biggest problem I am facing is that all images and links now have their source appear as if they are in a /book/ folder/directory even though they are in the root directory of the site. This may sound silly but is it worth me just making the rewrite point to "/" rather than "/book/" that way it appears as the root and not a folder? incrediBILL thank-you for the explanation, I think I wil be going for the hyphens then.
|
g1smd

msg:4506585 | 6:41 pm on Oct 10, 2012 (gmt 0) |
Make sure that all links to images and CSS files BEGIN with a leading slash, and mention the full path to the file. If you end anchor the pattern, the pattern will only match extensionless URLs. It will not attempt to rewrite requests for images or CSS files.
|
HaloPlayer

msg:4506910 | 1:44 pm on Oct 11, 2012 (gmt 0) |
Thanks for the reply g1smd. I'm in a bit of a pickle then because all my links and image sources don't start with a leading slash and there are thousands of links and images. I think another rewrite is called for images:
RewriteRule ^(.*)/(.*)/images/$ ../images/ Do you think this would be appropriate? This would fix the image problems, but not all the links. I now wish I had just put a slash at the beginning lol. Do you think maybe it would be better if I just went and added a slash to the beginnings of all links? It would take me a while but if it is the better option? Another option is it possible to do a rewrite rule to make instead of "www.example.com/book/motor_car" just become www.example.com/motor_car But then I guess the problem just changes links and images as /book/ would just become //
|
HaloPlayer

msg:4506914 | 1:50 pm on Oct 11, 2012 (gmt 0) |
I'll have a sift through this: [mydigitallife.info...]
|
g1smd

msg:4506915 | 1:56 pm on Oct 11, 2012 (gmt 0) |
I think another rewrite is called for images: RewriteRule ^(.*)/(.*)/images/$ ../images/ Do you think this would be appropriate? |
| No. Absolutely not. And especially not with that very dangerous code (there's at least four implementation errors in it). | I'll have a sift through this: |
| No. That's not the right answer. htaccess cannot change what the browser requests. Change what the browser requests by altering what is in the href="" part of the link. You need the links to images, CSS and JS files within your pages to begin with a leading slash so that their reference as seen from the web is root-relative. You need your rewrite to apply to requests for pages and not to requests for images or files. Trying anything else will lead to an infinite loop of chasing your own tail.
|
HaloPlayer

msg:4507413 | 1:17 pm on Oct 12, 2012 (gmt 0) |
I understand, I'll just have to add a leading slash to all my images & CSS files the old fashioned way, manual editing. There is one other option, create a folder/directory called /books and add the CSS, images. etc. into it, but that's cheating ;-) Could you see any issues with this? Really appreciate your efforts g1smd, a week ago I didn't even know what htaccess was and that any of this was possible :-)
|
HaloPlayer

msg:4510157 | 12:04 pm on Oct 20, 2012 (gmt 0) |
I almost forgot about the duplicate URL's. Is a RedirectMatch 301 the accepted method of redirecting? Should I also add [L,R=301] to the rule so that search engines know it has permanently moved?
|
g1smd

msg:4510203 | 6:18 pm on Oct 20, 2012 (gmt 0) |
Once you have a rule using RewriteRule from mod_rewrite in your htaccess file you must not use any directives from mod_alias. Specifically do not use Redirect or Redirectmatch in the same htaccess file as any RewriteRule directives. Convert all rules to use RewriteRule. When rules from different modules are listed within a single htaccess file, the rules are processed in "per module" order and not in the strict order they are listed in the htaccess file. Mixing rules from different modules means you have no control over the order they are processed. When that's a mixture of internal rewrites and external redirects processed by different modules that will usually end in disaster.
|
HaloPlayer

msg:4511640 | 10:44 am on Oct 24, 2012 (gmt 0) |
g1smd Thank you for your advice. I found the following from your hints on the mod_alias. This was an example on the redirect
RewriteEngine On RewriteCond %{REQUEST_URI} ^/page\.php$ RewriteCond %{QUERY_STRING} ^id=([0-9]*)$ RewriteRule ^(.*)$ http://mydomain.site/page/%1.pdf [R=302,L] There was also a second simpler example:
RewriteEngine On RewriteCond %{QUERY_STRING} ^id=([0-9]*)$ RewriteRule ^page\.php$ http://mydomain.site/page/%1.pdf [R=302,L] I'm wondering how I would go about it though because I am turning ?= into book
|
| This 47 message thread spans 2 pages: 47 ( [1] 2 ) > > |
|
|