Rewrite rule without changing URL in browser - apache

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]

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.

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.

Apache rewrite rules for creating user friendly urls

I am relatively new to rewrite and cant get the following to work. (clearly my domain is not testsite, but dont want my proper one going public as its not finished yet.)
i want to rewrite testsite/fishing/region/region.php?region=fife
to:
testsite/fishing/fife.php
then want to rewrite testsite/fishing/region/fishery/fishery.php?url=goldenloch
to
testsite/fishing/fife/goldenloch.php
I am using the following rules
RewriteRule ^fishing/([^/]*)\.php$ /fishing/region/region.php?region=$1 [L]
RewriteRule ^fishing/([^/]*)/([^/]*)\.php$ /fishing/region/fishery/fishery.php?url=$2&region=$1 [L]
each rule works on its own but when combined only the last one work. I have added the [L] flag which i believe should stop any other rewrite rules of the condition is met. however this still doesnt work.
I tried this and it seemed to work:
RewriteRule ^/fishing/([^/]*)\.php$ /fishing/region/region.php?region=$1 [R,L]
RewriteRule ^/fishing/([^/]*)/([^/]*)\.php$ /fishing/region/fishery/fishery.php?url=$2&region=$1 [R,L]
The only difference from yours (I think) is the leading / and I used redirects (R) just to see it was working. You should be able to remove the R so the user doesn't see the real url in his browser.
Just in case you're not, I would recommend using a command line tool (like curl) and not a browser to test this, just so you avoid any caching or other annoyances:
curl -vv 'http://localhost/fishing/fife/goldenloch.php'
You'll see the Location header in the server's response, that's the redirect at work. In my case, I see:
Location: http://localhost/fishing/region/region.php?region=fife
and
Location: http://localhost/fishing/region/fishery/fishery.php?url=goldenloch&region=fife
Turns out i needed to do
RewriteRule ^fishing/([^/]*)/([^/]*) fishery.php?url=$2&region=$1 [L]
RewriteRule ^fishing/([^/]*) region.php?region=$1 [L]
with the longer query first and it worked fine.

.htaccess Redirection for Querystrings

I have an old website that URLs were like /page.aspx?sch=XXXX&prg=YYYY. I want to redirect those old URLs to my new website's URLs. I'm trying to use an .htaccess file to do the trick but I couldn't get it work. What I want is:
/page.aspx?sch=XXXX&prg=YYYY ==> /page/sch/XXXX/prg/YYYY
If someone could helped me, I'd be very pleased.
Thanks...
If there are only these two URL parameters and they are only used in this order, you can do the following:
RewriteCond %{QUERY_STRING} ^sch=([^&]+)&prg=([^&]+)$
RewriteRule ^page\.aspx$ /page/sch/%1/prg/%2? [L,R=301]
Otherwise you will need to extract these parameters one by one and put them in in right order before doing that redirect.

The correct way to make a rewrite rule?

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