Making user-friendly URL using .htaccess - regex

I need to make my websites link user friendly. Currently, they look like this:
mydomain.com/post.php?title=News-Feeds-Latest-Addition-Puts-facebook-news-26
I want to make them look like this:
mydomain.com/title/get-back-your-recycle-bin-IT-news-85.html
I've written the following code into my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^title/([^/]*)\.html$ /post.php?title=$1 [L]
I don't know why, but it doesn't work. How can I get it to work?

You can use this code
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/post\.php\?(title)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=302,L]
RewriteRule ^(title)/([^.]+)\.html$ /post.php?$1=$2 [L]

Use this :
RewriteEngine on
RewriteRule ^title/get-back-your-recycle-bin-IT-news-85.html/?$ post.php?title=News-Feeds-Latest-Addition-Puts-facebook-news-26 [NC]
now you can usethis url : mydomain.com/title/get-back-your-recycle-bin-IT-news-85.html

Related

htacces regex to the following link

http://local.domain.ro/index.php?/login/login--login--login--produs--horn-metalic
I want to redirect it, permanent, in htacces to the following link:
http://local.domain.ro/produs/horn-metalic
I've searched for this topic, and try some answers but it doesn't help me.
EDIT
This is what I am trying:
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteCond %{QUERY_STRING} ^index.php?/login/login-_-login-_-login-_-produs-_-$ [NC]
RewriteRule ^/produs$ /produs/$1 [NC,L,R=301]
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} /index.php\?/login/login-_-login-_-login-_-([^-]+)-_-([^\s]+) [NC]
RewriteRule ^ /%1/%2? [NE,NC,R,L]

Redirection loop (URL rewriting)

I'm trying to rewrite my website URLs. I have:
http:// website.com/v2/message.php?ID2=123
I want:
http:// website.com/v2/message-123.php
I tried something in my htaccess but I've a redirection loop :'(
Here is my .htaccess:
RewriteCond %{QUERY_STRING} ^ID2=([0-9]+)$
RewriteRule message.php? http://website.com/v2/message-%1.php? [L,R=301]
Can someone help me with this?
I suggest not using .php in pretty URL, make it extension-less. Try these rules in v2/.htaccess:
RewriteEngine On
RewriteBase /v2/
RewriteCond %{THE_REQUEST} /message\.php\?ID2=([^&\s]+) [NC]
RewriteRule ^ message-%1? [NE,R=302,L]
RewriteRule ^message-(.+)/?$ message.php?ID2=$1 [L,QSA,NC]

String start with and end with anything (but is not .php)

Trying to make a redirection URL in htaccess.
I want to redirect URLs like
www.domain.com/pageANYTHING
to
www.domain.com
But I have an exception : when I got this
www.domain.com/page.phpANYTHING
do nothing.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^\.phpANYTHING [NC]
RewriteRule ANYTHING$ http://www.domain.com/? [L,R,NC]
Try this:
RewriteEngine On
RewriteRule ^page(?!\.php).*$ / [R=301,L]
In fact It works with a mix of your two propositions.
RewriteCond %{REQUEST_URI} !\.(?:php)$
RewriteRule ^page(?!\.php).*$ / [R=301,L]
Thank you so much guys.

Mod Rewrite and 404 Error

At the moment the users of my website can access files with the following url-schema:
http://www.example.com/dwl/download.php?file=/files/index.rar
Now i want that they can access it with the following schema:
http://www.example.com/dwl/content/files/index.rar
The download.php file is within the dwl directory.
I tried the following .htaccess:
RewriteEngine On
RewriteRule ^file/([^/]*)$ download.php?file=$1 [L]
But when i try it, i get a 404-Error.
Can anybody please help me to write the correct .htaccess-Entry?
If you want to redirect from http://www.example.com/dwl/download.php?file=/files/index.rar to http://www.example.com/dwl/content/files/index.rar, you need this:
RewriteRule ^/dwl/download.php?file=/(.*) /dwl/content/$1 [L]
I think the code you have to use is the following :
RewriteEngine On
RewriteRule ^/dwl/content(/files/[^/]*)$ /dwl/download.php?file=$1
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT/dwl directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /dwl/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dwl/download\.php\?file=([^\s&]+) [NC]
RewriteRule ^ content/%1? [R=301,L]
RewriteRule ^content/(.+)$ download.php?file=$1 [L,QSA,NC]

mod_rewrite rule to change parent folder

Some unchangeable hyperlinks on a website point to /folderA/index.php?id=somestuff.
I need to redirect the Request to /folderB/index.php?id=somestuff instead.
I did some experimenting with this but I just can't get it to work, any help is appreciated.
RewriteEngine on
RewriteRule /folderA/index\.php\?id=([\w-]+)$ /folderB/index.php?id=$1
Keep in mind that RewriteRule doesn't match query string and matches only REQUEST_URI without it. Use this code instead:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folderA/(index\.php\?id=[^&\s]+) [NC]
RewriteRule ^ /folderB/%1 [R=302,L]
Assuming that you are not planning on using different get request this should work:
RewriteEngine on
RewriteRule /folderA/index\.php\?id=(.*?)$ /folderB/index.php?id=$1