MOD Rewrite query string redirect issue - apache

I have a website xxxxx.com and the URL structure to dynamic content is
xxxxx.com/tours/index.php?tourid=x
where tour id is the key and x is the value
I've come up with this .htaccess rule that uses mod re-write to redirect traffic to the above URL to
xxxxx.com/x
where x is the value
Problem is:
When I go to the main URL ie. www.xxxxx.com/ or www.xxxxx.com/something.html it is also redirecting that to tours/index.php?
How can I just redirect requests to /tours/index.php?tourid=x to /x rather than everything?
Here is what I have now:
RewriteEngine On
RewriteRule ^([^/]*)$ /tours/?tourid=$1 [L]
I REALLY appreciate the replies. Thank you.

You could apply the rule to only files and folders which don't actually exist on the filesystem:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /tours/?tourid=$1 [L]

This will specifically target only the 'xxxxx.com/tours/index.php?tourid=x' URLs to /x, if you needed something else please comment.
RewriteEngine On
RewriteRule ^/tours/index.php\?tourid=(.*) /$1 [L]

Related

RewriteRule doesn't rewrite correctly

I'm hosting my development site on the localhost server and I used to access it at 127.0.0.1/dev. In the folder I have a .htaccess file containing following information. I'm new with the RewriteEngine and don't get this work.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php?page=$1
When I'm trying to access 127.0.0.1/dev/home, I simply get a message that the page doesn't found. I can't see where rewrite is redirecting me, so I can't debug the problem in easy way. I think that you can see the problem at the first look.
Thanks in advance.
Try this rule in /dev/.htaccess without leading slash in source pattern and target URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$0 [L,QSA]

htaccess rewrite rule does not work

I am facing problem with rewrite rule in htaccess file. Please help.
This is my current htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /product-project/
RewriteRule . index.php [L]
</IfModule>
Now I want to create a rewrite rule such as "product/category/id" should redirect to "product/profile?profile_id=id". For this I have used
RewriteRule ^product/.*/(.*)$ product/profile?profile_id=$1 [L,R]
which is working fine in some way.
1st problem -> It is redirecting properly, but URL also changes. It should stay as "product/category/10", if I use the id as 10. But when I use this, it is redirecting properly, but URL changes to "product/profile?profile_id=10". I read somewhere to use [L,P] instead of [L,R] , but it is giving as Server error.
2nd problem -> Now I want to create a new rewrite rule such as "product/id" should also redirect to "product/profile?profile_id=id". For this I have used
RewriteRule ^product/(.*)$ product/profile?profile_id=$1 [L,R]
Now this is not working & showing server error
3rd Problem -> Can I also create a new rewrite rule such as "/id" should also redirect to "product/profile?profile_id=id". Is this possible ?
4th Problem -> Can I also create a new rewrite rule such as "/id1/id2" should also redirect to "product/profile?profile_id=id1&serial_id=id2". Is this possible ?
Thanks in advance for your time.
Is this what you want please check.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1/$2?profile_id=$3
for two id's
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ $1/$2?profile_id=$3&id=$4
Use it..
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

.htaccess redirect for subdirectory

I have a help section on my site that I'm moving. It used to be in the root of the domain, now I'm moving it to a directory called "help". I would like to setup a redirect rule that keeps the url path.
Example: I would like
www.domain.net/topic1 to redirect to www.domain.net/help/topic1 etc...
Most solutions I have come up with end up being a redirect loop or do not keep the permalink.
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^$ help/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!help/).*)$ help/$1 [L,NC]
sadly, if the urls doesn't have anything in common... it seems you'll have to make one rule for each url then add a 302 (temporary) or a 301 (permanent) redirection.
i don't like too much the idea of one line for each url but this could work
Redirect 301 /topic1 /help/topic1
Redirect 301 /topic2 /help/topic2
you could try with something like this
RewriteCond %{REQUEST_URI} ^(topic1|topic2|....|topicN)$ [NC]
RewriteRule (.*) /help/$1 [R=301,L]

Simple mod_rewrite rule for adding /

I have an URL http://domain.com/test/main/getbts?_dc=13 and would like to add a / before the ?-mark in order for the URL to look: https://domain.net/test/main/getbts/?_dc=13
I tried the following Rewrite Rule:
RewriteRule ^test/main/getbts/\?_dc\=([^/]*)$ /test/main/getbts?_dc=$1 [L]
But it does not seem to work. My other rewrite rules (I have not used at the time I tested my first rule) are:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Could anyone give me a hint where to look or how to rewrite my rules?
Thank you very much in advance!
You can't match against the query string in a RewriteRule, just the URI. And in this case, you can just ignore it since it'll automatically get appended to the end. You just need:
RewriteRule ^admin/main/getcbts$ /admin/main/getcbts/ [L,R=301]
This redirects the browser so they see a / at the end of the URI in the location bar.

Redirect all page on a domain to one file

I am looking to permanently redirect all the pages on a domain to one page on the SAME domain, using htaccess/mod_rewrite.
Basically I want any page requested for the domain to return a holding page [ which is index.php] at domain.com/
most of my attempts so far are causing errors as they are throwing the server intoa loop.
Thanks in advance
.k
You need to exclude the destination you are redirecting to like this:
RewriteEngine on
RewriteRule !^index\.php$ /index.php [L,R=301]
If you just want to redirect requests that can not be mapped to existing files in the filesystem, add this condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ /index.php [L,R=301]
But you should rather respond with a 404 in that case.
Here's a simple implementation:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
And a bit more sophisticated one:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Both were taken from Zend Framework Zend_Controller Programmer's Reference Guide. Here's another useful doc Apache Module mod_rewrite.
ISR that this was asked here recently - but I can't find the q right now.
Why not just set up the 404 errorhandler to point to the holding page (and keep all other content out of the doc root)
C.
Thanks to all,
got it working, here it is it might be of help to others:
RewriteRule !^(index.php)|.(js|ico|gif|jpg|png|css|mp3|xml|swf)$ /index.php [L,R=301]
.k