url RewriteEngine not working as expected - .htaccess

I am tring to convert:
http://localhost/mysite/blog.php?slug=post-by-paritosh
To :
http://localhost/mysite/blog/post-by-paritosh
For that I am using .htaccess file with below rules
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?blog/(.*?)/?$ /blog.php?slug=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /blog\.php\?slug=([^\&\ ]+)
RewriteRule ^/?blog\.php$ /blog/%1? [L,R=301]
After checking syntax on http://www.htaccesscheck.com/ I found that it is ok but the rewrite are not working as expected.

You may try this .htaccess inside tgconnectmediaservice/ directory:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(tgconnectmediaservice/blog)\.php\?slug=([^&\s]+)\s [NC]
RewriteRule ^ /%1/%2? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?blog/([^/]+)/?$ blog.php?slug=$1 [L,QSA,NC]

Related

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

.htaccess : Rewrite url, one variable to directory

I would like to know if this final result can be achieved with some htaccess rules.
On the config folder. Where * is the page's name to show as folder.
/projectdir/config/?p=* or /projectdir/config/index.php?p=*
to /projectdir/config/*/
Example
/projectdir/config/?p=page1 => /projectdir/config/page1/
/projectdir/config/index.php?p=page1 => /projectdir/config/page1/
I tried the code posted in this question, but without results
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?p=$1 [NC]
Which basically: http://localhost/index.php?page=controller To
http://localhost/controller/
You can use this code in /project/config/.htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /project/config/
RewriteCond %{THE_REQUEST} /config/(?:index\.php)?\?p=([^\s&]+)&p2=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,NE,L]
RewriteCond %{THE_REQUEST} /config/(?:index\.php)?\?p=([^\s&]+)\s [NC]
RewriteRule ^ %1? [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p=$1&p2=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

Remove question mark from url using htaccess

I want to redirect the url : https://WEB_URL/video?id=1 to https://WEB_URL/video/1
and I have written following htaccess code but it is not working :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^video/([^.]*)$ video?id=$1 [QSA,L]
My full htaccess code is as follows:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
ErrorDocument 404 /login.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# To externally redirect /dir/abc.php to /dir/abc
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^video/([^.]*)$ video?id=$1 [QSA,L]
</IfModule>
why it is not working ? What I am doing wrong please help me on this.
Thanks in advance.
Try this:
ErrorDocument 404 /login.php
Options -MultiViews
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /video(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /video/%1? [R=302,L,NE]
# To externally redirect /dir/abc.php to /dir/abc
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^video/([^.]+?)/?$ video.php?id=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

url not rewriting i am using below code and mod_rewrite is also on

My url is like:
http://www.example.com/mock_profile.php?user_name=Yasin
but I want an seo friendly url like:
http://www.example.com/Yasin
I am using the below url RewriteRule in my httaccess but it is not working
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ mock_profile.php?user_name=Yasin
RewriteRule ^([a-zA-Z0-9_-]+)/$ mock_profile.php?user_name=Yasin
## Below is my all httacess code ##
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#RewriteRule ^(.*).html$ $1.php [QSA]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [NC,L,R=301]
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1
Try this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1 [L]
Explaination:
The following Condition tells the server to stop rewriting if the Request is for an existing directory.
RewriteCond %{REQUEST_FILENAME} !-d
And this tells the server to stop rewriting if the request is for a valid file:
RewriteCond %{REQUEST_FILENAME} !-f
If the request is not for a file /dir ,then rewrite it :
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1

SEF urls 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.

Resources