ReWrite Rule to a URL Friendly Page and User Profile - .htaccess

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>

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!

.htaccess: Redirect all but few urls to homepage

I've been recovering from a hack and there are thousands of spammy casino links from my site in the google index.
I want to redirect all urls except my legimitate pages to the homepage.
This is what I have now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(dienstverlening|starters|accountancy|administratie|belastingen|salarisadministratie|contact|over-ons|algemene-voorwaarden)
RewriteRule ^ [R=301,L]
</IfModule>
How can I have all other urls redirect to homepage in .htaccess?
You're missing a target in your rewrite rule. Apache's kind of dumb about parsing so it's going to assume that [R=301,L] is where you want to redirect to. Try adding a / in that rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(dienstverlening|starters|accountancy|administratie|belastingen|salarisadministratie|contact|over-ons|algemene-voorwaarden)
RewriteRule ^ / [R=301,L]
</IfModule>

Is there a way to setup a dynamic htaccess rewrite

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

htaccess rewrite help needed

I was trying to write a rewrite rule, but so far couldn't achieve what I want.
my site is in a subdirectory and the htaccess file is in that directory.
What I want is, if I go to http://example.com/site/ or http://example.com/site/index.phpit should rewrite to
http://example.com/site/index.php?key=2
The value of key won't change. It is fixed. I want it to load by default but hide from the url.
What I tried is,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?key=2&$1 [L,QSA]
</IfModule>
it's not working.
but if I go to address http://example.com/site/anything then it works.
Can you please help me in this?
I don't understand htaccess very well.
Thanks in advance.
Have it this way:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^(index\.php)?$ index.php?key=2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?key=2&$1 [L,QSA]
</IfModule>

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]

Resources