Forum Moderators: phranque
RewriteRule ^(.+)-something$ /$1.php
and the links are generated with the new extension automatically.
Now, I have some new pages, on the same site, and I can't give them the same extension (-something). If I rename the (e.g. /about_us.php) page into /info_about_us.php, can I make all the pages that start with "info_" have another, or no extension at all? If so, can someone please help.
I would like about_us.php to be just www.domain.com/about_us/ or www.domain.com/info_about_us/ if it's needed. Rather do it automatically.
Also, I want to ban Google from indexing the pages with .php extension, to void any dupe issues. The index page is index.php of course, but the site will not be linked as www.domain.com/index.php just www.domain.com/. Will this cause me any indexing errors? Is there a better way to do this?
thanks in advance,
What you are aking is possible, and actually easier than you think... (I think)
Since your new php page is different than -something you will not qualify for your first rule, so you can actually call your new page -anything, that is not -something then just add a second rule that 'catches' that request.
RewriteRule ^(.+)-something$ /$1.php [L]
RewriteRule ^(.+)-anythingelse$ /$1.php [L]
Rather than using the variable to the 'name' portion of the .php page, you might find it more effective to move it to the 'query string' protion, so you can run multiple html pages from one php page like this:
RewriteRule ^(.+)-anythingelse$ /mypage.php?show=$1 [L]
The [L] I added to both rules, says 'last rule, stop now' so as soon as a rule is matched your server does not try to match any more.
As for banning from the .php pages to avoid a dup penalty, that is possible, and a good idea... it involves {THE_REQUEST} and can be complicated, because, you are essentially doing a double rewrite, without looping.
Be glad to help you with that, but it's much easier to get one thing working, then add the other...
Hope this helps.
Justin
PS I don't know the structure of your pages, but you might want to try to find a more effecient version of the regular expression you are using... if your pages are only letters ([a-z]+) and [NC,L] at the end of the rule instead of just [L] is effective. If you have letters and numbers ([a-z0-9]+) and [NC,L]. (NC tells apache 'no case' when comparing letters, so both Page and page will match.
User-Agent: Googlebot
Disallow: /*.php$
The only probelm I'll have if Google starts snooping around (the files are .php on the server) or if someone links to them to harm me (extremely unlikely since no one knows the structure)
"As for banning from the .php pages to avoid a dup penalty, that is possible, and a good idea"
Houston, I have a problem:
widgetized terms
I have
RewriteRule ^(.+)-blah$ $1.php
and then somewhre down the httpd.conf
furniture furniture.php
test test.php
etc etc
if the (.+) is let's say furniture-store or anything that starts with "furniture", I'm always redirected to furniture.php (even though the URL is /furniture-store-blah. Can someone please help? I think I need to put a stop somewhere...
thanks in advance,
To transfer the whole string you could use:
RewriteRule ^(.+)$ /$1.php [L]
To transfer one string -something, as in your first example, then transfer all other strings as a complete string you could add a second rule and a condition:
RewriteRule ^(.+)-something$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} ^!.+\.php
RewriteRule ^(.+)$ /$1.php [L]
'!' is not
.+\.php 'anything' .php
First rule catches the specific pattern 'anything-something'
Second rule catches anything.
Condition excludes .php from the rewrite to avoid creating a loop.
Might want to look for a more efficient way to structure your rule/condition if you use them.
Again not really sure what you need, and unfortunately 'close doesn't count' with mod_rewrite.
Hope this helps and gives you some ideas.
Justin
RewriteCond %{REQUEST_FILENAME} ^!.+\.php
To work, this should actually read:
RewriteCond %{REQUEST_FILENAME}!^.+\.php
(IOW, the '!' needs to be outside the actual regex). That said, this...
RewriteCond %{REQUEST_FILENAME}!\.php$
...should also work. Note also that a space is required before the '!'. Sorry to butt in; please resume your regularly scheduled discussion. =)
Walkman,
You're going to need to be very specific about what 'forms' of the URL need to be redirected/rewritten and which don't. Mod_rewrite can only decide based on the regular-expressions pattern, and that has to be 100% correct if you want it to work.
Jim
some files are rewritten manually on the httpd.conf, and I want the rest to show with a different extension. Let's keep using the term furniture: domain.com/furniture.php needs be rewritten as www.domain.com/furniture . I have:
RewriteRule ^furniture furniture.php
RewriteRule ^beds beds.php
RewriteRule ^chairs chairs.php
IF the term is mentioned here, I want this to take priority. But if I use /beds-online-something, then it should go to (show) /beds-online.php, NOT beds.php. That is my problem. Everything that starts with beds* right now shows beds.php info (which is a dupe of www.domain.com/beds and so with other words. Essentially, I need only full matches for the manually entered rules and everything else gets
RewriteRule ^(.+)-something$ /$1.php
domain.com/not-on-rules shows the domain.com/not-on-rules.php page and so on.
thanks again,
I really appreciate your help and patience.
The reality is that may be more trouble than it is worth, because you still have to serve the information from somewhere, but is still possible.
EG Someone requests the page furniture.php, and we have to catch that request, then rewrite that to a page that does not exist, then serve the page that does not exist from the page that was already requested.
In order to make things easy on your self (and us) could we possibly add .php to the paths that do not have them... this would eliminate the duplicates, save a huge amount of learning necessary for you to do this, and would not effect indexing, as long as they do not have a query string (stuff after the?, like this page.php?variable=stuff).
If this will work for you, all you really have to do is change your links to page-of-stuff.php and use the rule/condition set from earlier:
RewriteCond %{REQUEST_FILENAME} !^.+\.php
RewriteRule ^(.+)$ /$1.php [L]
If this absolutely will not work for you, then this might work:
RewriteCond %{REQUEST_FILENAME} !^.+\.php
RewriteRule ^(.+)$ /$1.php [L]
RewriteCond %{THE_REQUEST} ^.+\.php\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
The first 'set' will provide a silent redirect to the .php file (the browser will not change, but the information will be served)
The second 'set' should catch only an original request for the files ending in .php and permanently redirect them to the file shown in the browser.
You should not need to, but if you get an error on the regular expression you might change it to [.+]
Not sure where the \\ came from in your example, but should not be there...
Please, make sure there is a single space between the '}' and '!' when you attempt to use the code.
If you try to use the second code, they have to appear in the order they are in.
In either case, you will have to make sure there are no other conflicting rules/conditions in your .htaccess file now.
If you are using any query strings (stuff after the ? this will not work.)
You might save yourself some headaches if you double check and make sure you only link to files in the way you decide to display them in the browser.
There is not too much else I can tell you without doing it for you.
Added: Don't mean this in a bad way, but I'm out of ideas, unless I can see exactly what is happening, I'm stuck... Obviously, if there is something little, that is no problem, but if you can't get any of it to work, I don't have too many more guesses.
Maybe someone else has a better idea of how to accomlplish this...
Hope this helps.
Justin
Fixed copy and paste error in the conditions.
I guess I have to rethink this whole thing and come back if needed because even non-existing pages, who should produce 404 errors, are still showing with the same template (no info, just the template).
No one will know furniture.php exists and I will ban Google from indexing .php files.
I noticed that if I add a slash, it works: RewriteRule ^furniture/ furniture.php
this seems to tell rewrite to stop there and not display the same page for all furniture* links (kinda full word match only) but if someone links to www.mydomain.com/furniture (without the / ) it will get a 404. Many might do that because we're used to directores. I guess I have to pick least worst one;)
I'm checking someone's site right now, and if I add domain.com/category/0000hfdeh-or-anything on the url, the same page shows. If someone wanted to do dupe damage, they could link from a geocities page. They don't have the slash on the link.
That is one of the easy ones...
you can make the / optional in your rule
RewriteRule ^furniture/? furniture.php
By adding the '?' you are saying 0 or 1 of the immediately preceding character or strings, and since there is no grouping of characters, in this case it will only apply to the '/' character.
This one should help, at least a little.
Justin
Added:
Please note, this could bring up the dup. question again, so you may just want to be sure people know how they need to write the link... otherwise we are back to complicated again.