The correct way to make a rewrite rule? - apache

I have a .htaccess in my site www folder that has this rewrite rule:
RewriteRule ^(\w+)/?$ /$1.php
It works, if you type in
http://sampardee.com/urltest -
It finds urltest.php and brings it up.
However, if you type in
http://sampardee.com/urltest/
it still brings urltest.php up but the CSS stops working. I have the CSS file specified in a link tag. The same results appear also when
http://sampardee.com/urltest.php/
is accessed.
Is there any way I can fix this so that someone could type in
http://sampardee.com/urltest/
and have urltest.php come up, but yet still display the linked CSS file?
Please help :)
-Sam

The problem isn't with mod_rewrite, but with the css link (the browser tries to fetch http://[...]/urltest/css/default.css instead of /css/default.css).
Try adding a beginning slash, and changing the to:
/css/default.css

A better idea would probably be to redirect http://sampardee.com/urltest/ to http://sampardee.com/urltest.
RewriteRule ^(\w+)/$ /$1 [R]
RewriteRule ^(\w+)$ /$1.php

Related

htaccess page to page redirect and seo friendly urls

i have a problem with a htaccess files and i cannot figure it what is the problem.
The site has url rewriting for seo purposes in place so:
www.website.com/page/seo-friendly-url
is rewritten to
www.website.com/page.php?seo=seo-friendly-url
this is done with the following
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
Now the problem is that i have to redirect some pages that are already indexed by the search engines to their new destination as they are no more available, for example:
www.website.com/page/seo-friendly-url
has to be redirected to
www.website.com/page/another-seo-friendly-url
I have tried something like this but it is not working
Rewriterule ^page/seo-friendly-url$ page/another-seo-friendly-url [R,NC,L]
also this one is not working
Rewriterule ^page/seo-friendly-url$ page.php?seo=another-seo-friendly-url [R,NC,L]
This seems pretty stupid but i can't find the problem :-/
Thank you for your help
Ema
Edit, for anubhava:
Hi,
no i have already set the rewriting for that.
What i'm trying to achieve is redirect an already rewrited link.
Let me explain myself better:
At the moment i have this url that is indexed by Google (or any other search engine) in the form of a beautified url (seo friendly). The url has this form:
www.website.com/page/seo-friendly-url
I have already set a rule in the htaccess so the previous link is rewritten and goes to a php page with a query string that is used to display some content.
The page and the query are in this form:
www.website.com/page.php?seo=seo-friendly-url
So basically i'm using the last part of the first url as a query parameter for the second url.
This is achieved (and works) through the following code here below:
RewriteEngine on
RewriteBase /
Rewriterule ^page/([a-zA-Z0-9_-]+)$ page.php?seo=$1 [NC,L]
So far so good.
Now what i need to achieve is to redirect this url, that has been deleted:
www.website.com/page/seo-friendly-url
to go to a new page
www.website.com/page/another-seo-friendly-url
Of course the same rules applies to this new url (www.website.com/page/another-seo-friendly-url -->is already rewrited to--> www.website.com/page.php?seo=another-seo-friendly-url)
What do i need to do to do the reewriting right?
Thanks
You need this extra rule before your existing rule:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+page\.php\?seo=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
Rewriterule ^page/([\w-]+)$ page.php?seo=$1 [NC,L,QSA]
Just add redirects like this:
RewriteRule page/seo-friendly-url /page/new-url [R=301,L]
Important: this rules have to be above your existing rewrites because of the L flag in your rewrites
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l
Edit
You want to redirect the old URL to avoid duplicate content (rewrite=internal, redirect=HTTP 301)
Maybe you are open for solutions thinking in another direction.
I would try to handle this in the application, no through rewrites. Right now the GET parameter seo is handled in page.php. Isn't it an idea to extend this in that way one product can be identified through multiple seo aliases? If one product has to be taken off a similar one will then own this alias (simply a change of one row in the database).
As I don't know what software you are using this may be not possible.

Writing a RewriteRule with just one exception

For a project I need to write a RewriteRule in a .htaccess file but unfortunately I have nearly no experience how to write such rules. What I want to do is rather simple: A complete redirect from one path to another with just one exception. Let me show you my try:
RewriteCond %{REQUEST_URI} !^/success$
RewriteRule ^.*/wop_de/checkout/onepage.*$ http://%{HTTP_HOST}/wop_de/onestepcheckout/ [R=301,L]
What I have thought: If there is no string like "/success" in the URL do a redirect from "/wop_de/checkout/onepage/" to "/wop_de/onestepcheckout/". Well, I guess I was thinking the wrong way as it doesn't work. Could you help me, please?
Also do you know a good tutorial to learn how to write such rules? Thank you in advance!
Use this rule:
RewriteRule ^wop_de/checkout/onepage.*$ /wop_de/onestepcheckout/ [R=301,L]
.htaccess is per directory directive and Apache strips the current directory path (leading slash) from RewriteRule URI pattern.

Redirecting URL with (Brackets)

does anyone know how I can redirect a URL with brackets? I made a mistake a while back and we have since changed to a proper url format but the link with URL remains indexed in Google.
I've tried the following but none seems to be working
RewriteRule ^url-with-(brackets)(.*)$ http://url.com/url-with-no-brackets$1 [R=301]
RewriteRule ^url-with- \brackets\ (.*)$ http://url.com/url-with-no-brackets$1 [R=301]
Thanks in advance.
Give this a try:
RewriteRule ^(.+)\((.+)\)(.*)$ /$1$2$3 [R=301,L]
This will extract the entire text without the parenthesis.
Keep in mind that you may have been cached from previous attempts since you are using 301 so I recommend you that you test it using a different browser just to make sure its working.

Rewrite rule without changing URL in browser

I want a rewrite rule to work in such a way that it loads the second URL but in browser address field it should show first URL.
For example domain.com/folder1/folder2 should load domain.com/folder1 but not show domain.com/folder/folder2 in browser.
I tried this but it basically changes the URL in browser.
RewriteRule ^/folder1/folder2(.*)$ /folder1/$1 [L]
Tried googling but didn't get any help. Appreciate your help!
Remove the beginning /s from the rule:
RewriteRule ^folder1/folder2(.*)$ folder1/$1 [L]

mod_rewrite and hyperlinks

I'm trying to get my head around mod_rewrite and friendly URLS.
OK, on a very basic level I have the following rule:
RewriteRule ^register$ register.php [L]
This allows me to browse to www.mydomain.com/register
The hyperlink within my pages shows register.php. Do I have to manually change my links to register?
Esentiallly, I do not want the .php extension on any of my links.
Thanks!!
Yes, you must manually change the hyperlinks (or use your favourite search-and-replace tool). mod_rewrite can't do this for you; it can only rewrite incoming requests, not outgoing HTML.
Yes, you'll need to change the URL in your code if that's not what you want to show up in the address bar.
Just an addition:
Note that RewriteRule ^(.*)$ /$1.php rewrites all files for you which saves you typing a lot of rules ;) Offcourse you can add more validation to it by using something like RewriteRule ^(.*)$ /index.php?pageId=$1.