Rewrite article.php?title=variable to variable/ - .htaccess

I'm trying to redirect article.php?title=variable to variable/ using .htaccess but I'm not really sure what is wrong with my code. If I go to http://site.com/variable/ it is redirecting me to http://site.com/article.php//?titlu=article.php
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#Rewrite urls for article
RewriteRule ^(.*)/$ article.php?titlu=$1
RewriteRule ^(.*)$ /$1/ [R]
</IfModule>

You have to capture the query string. This should work for you.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} title=([A-Za-z0-9_.-]*)
RewriteRule ^article.php /%1/? [R=301,L]
I'm assuming you don't want to redirect slashes... if you do you could add them to the string as well (or accept all characters).
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} title=(.*)
RewriteRule ^article\.php /%1/? [R=301,L]

Related

Add a specific part to url using .htaccess

I guess it's an easy question but I hardly know syntax used in .htaccess, so I got a bit stuck. How to add '/de/' after a domain name in this case, so the result will be something like 'blablabla.com/de/blog' or 'blablabla.com/de/offices' instead of 'blablabla.com/blog' and 'blablabla.com/offices'?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=de [NC]
</IfModule>
I know that there should be RewriteRule after RewriteCond but, as I've said before, I'm not familiar with the syntax...
You can use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} lang=de [NC]
RewriteCond %{REQUEST_URI} !^/de/ [NC]
RewriteRule ^(.*)$ /de/$1 [R,L]
</IfModule>
To access your site urls with /de segment ,you can use :
RewriteEngine On
RewriteBase /
RewriteRule ^de/(.+)$ /$1 [NC,L]

Need help writing htaccess rule

Currently my localhost URL is like localhost/apnaujjain/page.php?page_id=1&action=contacts
Now I want to write a rule so that when I go to above url it just rewrite the url like localhost/apnaujjain/contacts.html
See it should convert .php into .html without affect page content.
Until now I've tried
Options +FollowSymLinks -MultiViews
RewriteEngine on
#RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.php?page_id=1$&action=2$
RewriteRule ^(.*)\.php $2.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
But it's not working.
You can use this code in /apnaujjain/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to
# localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^.]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

Safely move my blog?

My blog is currently located at:
example.com/blog
with posts being something like:
example.com/blog/my-post
I want to edit my htaccess file so anyone that accesses
example.com/blog/my-post
would end up at:
example.com/my-post
How can I do this with an htaccess file?
UPDATE
Right now I have:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
That will forward any non-www traffic to www. However, I need to modify this to support /blog as well. I don't think my syntax is right:
RewriteCond %{HTTP_HOST} ^example\.com/blog$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
?
You cannot match /blog using HOST_NAME variable.
Have your htaccess like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
RewriteRule ^blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
This assumes there is no .htaccess in /blog/ directory. However if it is there for some reason then use this rule as first rule in /blog/.htaccess:
RewriteEngine On
RewriteRule ^(.*)$ /$1 [L,R=301]

rewrite rule for shop in lithium / apache

I have a lithium installation and all the .htaccess works fine.
I need to install OpenCart as a shopping cart in app/webroot/shop
I copied all the files and also changed the .htaccess file in the root folder of lithium installation as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule shop/(.*) /app/webroot/shop/$1 [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Still when I browse http://domain.com/shop it takes me to http://domain.com/app/webroot/shop/
With an error on page:
Exception
lithium\action\DispatchException (code 404)
Action `webroot` not found.
Please help me in solving this problem.
You may try this instead:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/app/webroot/shop/? [NC]
RewriteRule ^shop/(.*) /app/webroot/shop/$1 [L,NC]
RewriteCond %{REQUEST_URI} !/app/webroot/? [NC]
RewriteRule ^$ /app/webroot/ [L,NC]
RewriteCond %{REQUEST_URI} !/app/webroot/? [NC]
RewriteRule ^(.*) /app/webroot/$1 [L,NC]
</IfModule>
The problem is that your last "catch all" rule is also redirecting all requests for the shop to lithium, which you don't want.
Try this
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Rewrite all URLs that start with /shop to /app/webroot/shop
RewriteRule ^shop/.? /app/webroot/shop%{REQUEST_URI} [L]
# Rewrite all URLs that don't start with /app/webroot/shop to /app/webroot
RewriteCond %{REQUEST_URI} !^/app/webroot/shop
RewriteRule .? /app/webroot%{REQUEST_URI} [L]

Redirecting with htaccess

It seems a very eask task, but I couldn't make it for hours after reading too much tutorial. Please help..
I need to redirect
http://example.com/myfolder/myfile.php?type=1&add=20
to this address:
http://example.com/newfolder/mytasks.xml
I tried too many. My last tryout was this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/myfolder/myfile.php?type=1&add=20$ /newfolder/mytasks.xml [R=301,NC,L]
</IfModule>
Use this rule instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+myfolder/myfile\.php\?type=1&add=20 [NC]
RewriteRule ^ /newfolder/mytasks.xml [R=301,L]
Remember:
RewriteRule match doesn't start with a slash /
RewriteRule doesn't match query string, it matches only Request URI
If you also want the query type=1&add=20 removed, something like this should work:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} type=1&add=20 [NC]
RewriteCond %{REQUEST_URI} !/newfolder/ [NC]
RewriteRule ^myfolder/myfile\.php /newfolder/mytasks.xml? [R=301,NC,L]
Redirects:
http://example.com/myfolder/myfile.php?type=1&add=20 to
http://example.com/newfolder/mytasks.xml
For silent mapping, replace [R=301,NC,L] with [NC,L]

Resources