.htaccess: how to only rewrite .php URL? - .htaccess

My rewriterule with a condition is working fine as below:
http://www.sitename.com/index.php?n=text redirects to
http://www.sitename.com/pages/text
and the page renders properly, however, there is a problem that with the redirected URL the arguments are also added to the URL. So actually in address bar it looks like-
http://www.sitename.com/pages/text?n=text
Could anyone help me on this? The htaccess code is given below.
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php http://www.sitename.com/pages/%1 [r=301,nc]

You probably want to catch "index.php.*". Otherwise mod_rewrite only replaces the "index.php" part of the URL "index.php?n=text" with the new URL.

Guss,
From what you suggested, i reconstructed it as follows:
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php.* http://www.sitename.com/pages/%1 [r=301,nc]
This doesnt seem to be working either. Can you please elaborate on what you have said?
thank you
aditya

donĀ“t use the url in the rewrite rule, apache then sends a http 200 code and then the 301...
try sth. like this:
RewriteRule (index\.php)(?n=)(.*) /pages/$3 [r=301]

Related

.htaccess multiple redirects with fallback

I'd like to create some kind of redirection script using .htaccess to map short urls (like example.com/1 to other urls). To do so, I've created this:
RedirectPermanent /1 https://www.youtube.com/watch?v=...
RedirectPermanent /2 https://www.youtube.com/watch?v=...
So far, that's working. However, it's missing a fallback. I'd like to add a 404 page that is shown whenever someone tries to navigate a URL that doesn't have any redirect (yet).
I've tried adding this:
...
RewriteCond %{REQUEST_URI} !^/404.html$
RewriteRule .* /404.html [L,R=302]
Obviously, this isn't working, because now ALL calls are redirected to 404.html. I thought about adding the redirects as conditions, but since there might be many redirects that approach seems very bad to me.
What can I do instead?
Thanks for helping out.
I've done this now, but I'm not sure if this is the best solution.
Please comment if you have any advices.
RewriteRule ^/1$ https://www.youtube.com/watch?v=1 [L,R=301]
RewriteRule ^/2$ https://www.youtube.com/watch?v=2 [L,R=301]
RewriteCond %{REQUEST_URI} !^/404.html$
RewriteRule .* /404.html [L,R=302]

How can I 301 redirect a URL with variables in it (.htaccess)?

I have the following URL:
https://www.parcelparcel.com/nl_NL/pakket-versturen-frankrijk/?page=nl_NL/populair/pakket-naar-frankrijk/
Which I would like to 301 redirect to:
https://www.parcelparcel.com/nl_NL/pakket-versturen-frankrijk/
I tried to pull this off with the following code in my .htaccess:
RewriteRule ^index.php/populair/pakket-naar-frankrijk/?page=nl_NL/populair/pakket-naar-frankrijk/?$ https://www.parcelparcel.com/nl_NL/pakket-versturen-frankrijk/ [R=301,L]
However, without results. Can anybody advice me or tell me what I'm missing. I checked if the URL is redirected in incognito.
Thanks in advance!
Try This:
RewriteCond %{QUERY_STRING} !^(.*)$
RewriteRule ^nl_NL/pakket-versturen-frankrijk/?$ nl_NL/pakket-versturen-frankrijk/?page=nl_NL/populair/pakket-naar-frankrijk/ [L]
Fisrt line to exclude a request that has Query String
The second to choose a URI that starts with nl_NL/pakket-versturen-frankrijk/ or nl_NL/pakket-versturen-frankrijk and redirect it internally to nl_NL/pakket-versturen-frankrijk/?page=nl_NL/populair/pakket-naar-frankrijk/

Redirect 301 using .htaccess

I need to redirect from page
http://masterkanz.com.ua/index.php?route=product/category&path=79_103
to
http://masterkanz.com.ua/pishushchie-prinadlezhnosti/ruchki-sharikovye
Rule like
Redirect 301 /index.php?route=product/category&path=79_103 http://masterkanz.com.ua/pishushchie-prinadlezhnosti/ruchki-sharikovye
doesnt works. I read that problem can be in symbol "?". But what should I do?
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^route=product/category&path=79_103$
RewriteRule ^index\.php/?$ http://masterkanz.com.ua/pishushchie-prinadlezhnosti/ruchki-sharikovye? [R=301,L]
I've tried this and it is working, you need to use {QUERY_STRING} to get the rule working.
I hope this helps.

htaccess URL Rewrite for products doesn't redirect me

There is plenty of information out there but nothing I've read on the interwebz has given me an answer as to why my htaccess is not working.
I cannot determine why my rule isn't rewriting the URL as I thought it would. I have the following url:
domain.com/Book/bookpage/index.php?bookID=123&bookName=foo_bar
I would like to change it so that when someone hits that URL, it shows like:
domain.com/Book/123/foo_bar
I started off trying to get it to work using just the Book ID and haven't even gotten that to work.
This is what I have thus far:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2
However, after placing that htaccess in the root of the site and going to the URL:
domain.com/Book/bookpage/index.php?bookID=123
The URL in the address bar remains the same.
try this:
RewriteEngine On
RewriteBase /
# this rewrite domain.com/Book/123 or domain.com/Book/123/
RewriteRule ^Book/([0-9]+)/?$ /Book/bookpage/index.php?bookID=$2 [L,NC,QSA]
# this rewrite domain.com/Book/123/title or domain.com/Book/123/title/
RewriteRule ^Book/([0-9]+)/([a-z0-9\-_]+)/?$ /Book/bookpage/index.php?bookID=$1&bookName=$2 [L,NC,QSA]
Try adding [L,R=301] at the end of the line:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2 [L,R=301]

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

Resources