Make url-rewrite in .htaccess file to work both ways - .htaccess

I have the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC]
</IfModule>
when my url format is something like:
mysite.com/project/1/test/vw
everything works fine. But I would also like when the url is typed in the other format, to be rewritten to the first format.
i.e. When I type the url like this:
mysite.com/project.php?id=1&pName=test&urlParam=vw
I wants it to be turned into:
mysite.com/project/1/test/vw
Do I have to write another rule? or is it accomplished in some other way?

The current rewrite rule would not handle both the cases. Add the below rewrite rule as well.
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlParam=$3&no-redir [L,NC,QSA]
RewriteCond %{QUERY_STRING} !no-redir
RewriteCond %{QUERY_STRING} (^|&)id=([^&]+)&pName=([^&]+)&urlParam=([^&]+)
RewriteRule ^project\.php$ project/%2/%3/%4? [R=301,L,NC]

You need one more external redirect rule. Use this code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /project\.php\?id=([^&]+)&pName=([^&]+)&urlPram=([^&\s]+) [NC]
RewriteRule ^ project/%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([0-9]+)/([a-zA-Z0-9]+)/(.+)?$ project.php?id=$1&pName=$2&urlPram=$3 [L,NC,QSA]
</IfModule>

Related

Multiple htaccess rewrite conditions and rules?

Having some issues when adding a new condition and rule to my htaccess file. We are needing to redirect traffic from a specific referral domain. Existing htaccess looks like this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /~bayour8/
RewriteCond %{REQUEST_FILENAME} .*\.(jpeg|jpg|gif|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /~bayour8/public/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~bayour8/index.php [L]
#suPHP_ConfigPath /home/bayour8/public_html
</IfModule>
I need to add something like this but it never processes it:
RewriteCond %{HTTP_REFERER} !^http://domainx.com [NC]
RewriteRule ^/?first-page.html$ http://www.domainy.com/pages/pagename/ [L]
Any assistance would be appreciated!

Masking an url in rewrite

I have a url
www.abc.com/description.php?id=123035
when I write a url like
abc.com/123035/Four_Seasons_Hotel_Istanbul_at_Sultanahmet
I want to get the result. Rewrite won't see "Four_Seasons_Hotel_Istanbul_at_Sultanahmet" will see only id and will work.
How can that be done?
I have tried this
Options +FollowSymlinks
# Prevent Directoy listing
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)$ description.php?id=$1 [QSA]
</IfModule>
But it doesn't work.
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/[^/]+/?$ /abc/description.php?id=$1 [L,QSA]

.htaccess retrieve from folder if subdomain, not 301 redirecting?

I want my site to just point to /user folder if the request is a subdomain.
If the request is subdomain.site.com/admin, then the site should show the page for subdomain.site.com/user/admin.
The problem with my code is that it makes an 301 redirect instead of just keeping the url-address.
My code look like this:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se(.*)
RewriteCond %{HTTP_HOST} ^[^.]+.bildsida.se(.*)
RewriteRule ^$ user/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) user/$1 [L]
</IfModule>
And you can try for yourself, go to http://mickes.bildsida.se/admin and see how the address changes to /user/admin. Very disturbing...
You just need a few of the lines you showed to get this working.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} ^.+\.bildsida\.se
RewriteRule ^(.*)$ user/$1 [L]
I adjusted the HTTP_HOST checks because that parameter only looks at the domain name, so you don't need the (.*) at the end. I also removed the checks that look if the file exists or is a directory, since you want everything redirected (no reason to make it possible to access files from other subdomains, for example)
You code can be simplified to these lines : (try them instead)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?bildsida\.se [NC]
RewriteRule ^(.*)$ /user/$1 [QSA,L]
Finally! After days and nights of reading forums and documentations, and testing all possible ways, I got it to work!
Sulotion:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} !^bildsida.se
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ user/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? user/$1/ [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.+)/ user/$1 [QSA]
</IfModule>

.htaccess check for specfic query string attribute

We are changing a sites' structure and the old urls look like:
http://www.website.co.uk/pages.php?PageId=7
The new url is:
http://www.website.co.uk/niceurl
And I have this .htaccess that comes with the CMS I am using so the core functionality of it cannot be made to work differently.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^(.*)$ index.php/$1
</IfModule>
What I really want to do is check if pages.php is getting requested and if so ignore the rule:
RewriteRule ^(.*)$ index.php/$1
Then after this rule do something like:
RewriteRule ^PageId=7(.*)$ /niceurl [R=301]
Any help would be appreciated.
The solution:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteCond %{QUERY_STRING} !(PageId=7)
RewriteRule ^(.*)$ index.php/$1
RewriteCond %{QUERY_STRING} PageId=7
RewriteRule .* /niceurl? [R=302,L]
</IfModule>

Mod_rewrite question

It is possible to rewrite my url using mod_rewrite and htaccess where a user could enter:
http://www.mainsite.com/12345
which the server would translate as:
http://www.mainsite.com/index.php?ID=12345
By also trying to remove the file extension from all pages, (remove .php) it conflicts and causes a never ending redirect:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.php$ $1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]+)$ index.html?ID=$1 [QSA,NC,L,R]
</IfModule>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ index.php?ID=$1

Resources