Is there a way to setup a dynamic htaccess rewrite - .htaccess

I'm need some help with an htaccess rewrite. I would like to do a clean url setup that is dynamic.
For example
mydomain.com/pagename/var1/var2/
mydomain.com/pagename2/var1/var2/var3/etc
mydomain.com/pagename3/var1/var2/var3/etc
I would like the first level to be alway be the filename in the rewrite?
mydomain.com/pagename.php?v1=var1&v2=var2&v3=var3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$0\.php -f [NC]
RewriteRule ^([^.]+)/?$ $0.php?v1=$1&v2=$2&v3=$v3 [L]

This will do your job done,
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^home/{0,1}$ pagename.php?v1=1[QSA,L]
RewriteRule ^sone/{0,1}$ pagename.php?v1=2[QSA,L]
RewriteRule ^music/{0,1}$ pagename.php?v1=3[QSA,L]
RewriteRule ^video/{0,1}$ pagename.php?v1=3[QSA,L]
</IfModule>
and the link look like that: mydomain.com/home but the real link is mydomain.com/pagename.php?v1=1

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!

ReWrite Rule to a URL Friendly Page and User Profile

I want to use two RewriteRules to redirect Pages and Users to the right link but I'm coundn't achieve what I want, maybe someone could help me...
I want to redirect any Url Friendly page from:
http://www.example.com/my-url-friendly-page
to
http://www.example.com/index.php?cod=my-url-friendly-page
AND User Profiles from:
http://www.example.com/profile/username
to
http://www.example.com/profile/index.php?user=username
This is my htaccess, the first rule is working ok but I couldn't fix the second one...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1
RewriteRule ^/profile/(.*)$ index.php?user=$1 [QSA,L]
</IfModule>
Can anyone help me with this ?
Reorder your rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile/(.*)$ index.php?user=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1 [L,QSA]
</IfModule>

UrL Rewrite using code with my domain

URL : http://domainname.com/index.php?p=top-games
I need to rewrite this as http://domainname.com/top-games
How I do this using htaccess file? Please can any one give me the htaccess code.
Thanks
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]
</IfModule
Create .htaccess file in your root folder and paste code
You're probably looking for redirect in reverse direction.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?q=$1 [L,QSA]

How to redirect using .htaccess?

I need my site to redirect from this url:
mysite.com/old/dir/param1/param2/...
to
mysite.com/dir/param1/param2/...
I need to keep the params, just remove the "old" in the url.
How can I add this rule to my .htaccess?
Thanks!!!
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]
</IfModule>
The right rule for it was
and I had to place it before all the other rules. So my .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old/dir/(.*) /dir/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Thanks anyway! :)
You could just write it like this.
Rediret 301 mysite.com/old/dir/param1/param2/(.*) mysite.com/dir/param1/param2/$1
If you're planning to redirect the entire directory of old/dir to dir/, then try this:
Redirect 301 mysite.com/old/dir/(.*) mysite.com/dir/$1
The next option you could try is, Url Rewriting.
Assuming that you have set your Rewrite Engine on and have set the Rewrite Base, add these lines:
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]

Conditional rewriting in htaccess file

I'm rather new to the whole .htaccess thing and I'm using the following right now to use 'pretty url's':
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
</IfModule>
Now i found my website a bit slow and decided to start gzipping my CSS files thru a php script I found somewhere on the web (the website). For this to work I need to rewrite the url to open the correct php file. That would look something like this:
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
But I only want the first to happen when the second doesn't and vice versa. In other words i'd like something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
if request doesn't contain .css do
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
else do
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Can anyone help me with the proper code or a place where i can find a way to use some kind of conditional statement in htaccess files?
Thanks in Advance!:)
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !.css
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Also, you can put the last rule on top

Resources