SEF urls htaccess - .htaccess

Currently my url structure is like:
http://mysite/index.php?page=main
http://mysite/index.php?page=bowling
http://mysite/index.php?page=inn
How to optimize this structure by leaving just (main,bowling,inn respectively) ?

Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R]
RewriteCond %{THE_REQUEST} \ /+index\.php\?page=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)/?$ /index.php?page=$1&id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
make sure mod_rewrite is turned on.

Related

remove inside url some element via htaccess

Via an htaccess, I want to remove this point inside url : Products/Description
example : https://test.com/Products/Description/logitech/Id-12
I tried this but it does not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
below is my complete htaccess tried rules file:
RewriteEngine On
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Remove Products/Description ===> not working
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
With your shown samples and efforts, please try following .htaccess rules file. We need to do few changes in it. Make sure that your .htaccess file is residing along with Products folder(not inside it, besides it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /Products/Description/
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
RewriteCond %{THE_REQUEST} \s/index\.php\?([^&]*)&([^&]*)&Id=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Products)/(Description)/(\d+)/?$ index.php?$1&$2&Id=$3 [QSA,NC,L]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Rewrite matching URL

I'm trying to rewrite matching URLs of my website but it's not working for some reason, don't know why...
Here is current URL:
example.com/details.php?p=10&text=some-text-is-here
want to write like this
example.com/post-details/10/some-text-is-here
My .htaccess is here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/details\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /details/%1? [R=301,L]
RewriteRule ^details/([0-9]+)$ /details.php?id=$1 [L]
any help would be appreciated
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/details\.php\?p=(\d+)&text=(.+)\s [NC]
RewriteRule ^ /post-details/%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteRule ^post-details/(\d+)/(.+)$ /details.php?p=$1&text=$2 [L,NE]
Note: clear browser cache the test

Simple .htaccess rewrite for single get parameter

How can I rewrite
http://data.example.com/?file=test
to
http://data.example.com/test
via .htaccess?
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /\?file=([^\ &]+)
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?file=$1 [L,QSA]

htaccess seo-friendly URL's

I need some help to get friendly URL's on my website.
These are my links i need to rewrite:
www.example.com/index.php?page=pagename
www.example.com/index.php?page=profile&id=number
This is my .htaccess. I solved the issue for the first link, however I can't do it for the second one.
I would like to have for the second link:
www.example.com/profile/name or
www.example.com/profile/id
This is my .htaccess file:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{THE_REQUEST} \?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
RewriteRule ^ /%1? [L,R=301]
# rewrites back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
The code I submitted above solved my problem.
You can use:
# for www.example.com/index.php?page=profile&id=number
RewriteCond %{QUERY_STRING} page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]
# for www.example.com/index.php?page=page
RewriteCond %{QUERY_STRING} page=([^&]+)
RewriteRule ^ /%1? [L,R=301]
And for rewrites back
# rewrites back
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

How to set pretty url with .htaccess for different parameters

I'm trying to set pretty URLs for my website. Thanks to #JonLin I managed to change site with two parameters. The other thing I want to do is make it work for one parameter as well. Here is the code
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+devplus0/index\.php\?key=([^&]+)&lang=([^&\ ]+)
RewriteRUle ^ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?key=$2&lang=$1 [L,QSA]
You can check the site prettyurl from http://www.devplus.co/english/about-us
For one param, add:
RewriteCond %{THE_REQUEST} \ /+devplus0/index\.php\?key=([^&\ ]+)
RewriteRUle ^ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ index.php?key=$1 [L,QSA]

Resources