.htaccess redirection rules with parameter rewrite - apache

I have the following .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Which redirects every server/path request to server/path.php file.
I want to add one more specific rule, to redirect everything of the form /item/id to /item.php?var=id
Can I make it work together?
Appreciate the help.

Try with below rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?var=$2 [NC,L]

Related

Complex .htaccess rewrite

can you please let me know htaccess code to redirect there urls:
https://www.example.com/forum/news/state-news/archive to https://www.example.com/forum/news/state-news?archive=yes
https://www.example.com/forum/news/state-news/city-news/archive to https://www.example.com/forum/news/state-news/city-news?archive=yes
https://www.example.com/forum/news/state-news/city-news/area-news/archive to https://www.example.com/forum/news/state-news/city-news/area-news/?archive=yes
My tried htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css.php$ core/css.php [NC,L]
RewriteRule ^install/ core/install/ [NC,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?routestring=$1 [L,QSA]
</IfModule>
Have your htaccess Rules file in following way. I have specifically covered 3 URLs shown by you in your question. Please make sure you use these Rules and make sure you clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css\.php$ core/css.php [NC,L]
####I have commented below rule that doesn't make sense, since NO right part in it only condition is mentioned.
####RewriteRule ^install/core/install/ [NC,L]
###New rules added by me to handle 3 different URLs shown by OP in question.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(forum/news/state-news(?:(?:/city-news(?:area-news))))/(archive)/?$ $1?$2=yes [NC,QSA,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?routestring=$1 [L,QSA]
</IfModule>

Error come with seo friendly url using .htaccess

Firstly I have coded my website in localhost than everything goes fine all URL work but when I upload it on the cloud (GCP) the URL is not working(It shows that the URL is not found)
the following code is of .htaccess file in localhost
RewriteEngine On
RewriteBase /blog_site/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^single_post/(.*)$ single_post.php?post_title=$1
RewriteRule ^single_post/(.*)/ single_post.php?post_title=$1
RewriteRule ^category/(.*)$ category.php?category=$1
RewriteRule ^category/(.*)/ category.php?category=$1
RewriteRule ^author/(.*)$ author.php?author=$1
RewriteRule ^author/(.*)/ author.php?author=$1
RewriteRule ^author/(.*)/(.*)$ author.php?author=$1&user=$2
RewriteRule ^author/(.*)/(.*)/ author.php?author=$1&user=$2
RewriteRule ^search/(.*)$ search.php?search=$1
RewriteRule ^search/(.*)/ search.php?search=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Also use base tag
<base href="/blog_site/" target="_self">
On cloud server the code of .htaccess file is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^single_post/(.*)$ single_post.php?post_title=$1
RewriteRule ^single_post/(.*)/ single_post.php?post_title=$1
RewriteRule ^category/(.*)$ category.php?category=$1
RewriteRule ^category/(.*)/ category.php?category=$1
RewriteRule ^author/(.*)$ author.php?author=$1
RewriteRule ^author/(.*)/ author.php?author=$1
RewriteRule ^author/(.*)/(.*)$ author.php?author=$1&user=$2
RewriteRule ^author/(.*)/(.*)/ author.php?author=$1&user=$2
RewriteRule ^search/(.*)$ search.php?search=$1
RewriteRule ^search/(.*)/ search.php?search=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
and the base tag is
<base href="/" target="_self">
The VM instance link of GCP such that you can see how website behave
website link
If I manually type the url http://your_IP/contact.php working. But if I use http://your_IP/contact is not working
Based on your shown samples only, could you please try following Rules once. Please make sure you clear your browser cache before testing your URLs. There were multiple issues in your htaccess file, like conditions were not there, multiviews(which we came to know later in comments was ON) which made issues in your code.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^single_post/(.*)/?$ single_post.php?post_title=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^author/([^/]*)/(.*)/?$ author.php?author=$1&user=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(category|author|search)/(.*)/?$ $1.php?$1=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ $1.php [L]
EDIT by OP:
Please before you go forward please check .htaccess is enable it. If you don't know is enable or disable then type ---- in your .htaccess file, if it shows 500 internal error then its enable or it's disable. Then try to enable by searching on internet.

Renaming file in URL (.php file)

I have a file called filename-step1.php and looking to make the url domain.com/filename/step1 through .htaccess.
My current .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
At the moment, this works fine for removing the .php tag from the url. Just need to fix the directories.
You could add a RewriteRule to redirect to $1/$2.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /$1-$2.php [NC,L]
</IfModule>
So, filename/step1 will trigger the filename-step1.php file.
I am giving 2 solutions here, please try one at a time only in your .htaccess file. With your shown samples, could you please try following as a Generic rules.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1-$2.php [NC,L]
OR in case you are looking for specifically url filename/step1 try following.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteRule ^(filename)/(step1)/?$ $1-$2.php [NC,L]

.htaccess redirect index.php but 404 if without .php

so the thing is that if i write 'localhost/mvc/' or 'localhost/mvc/index.php' it works fine but if i type 'localhost/mvc/index' it returns a 404 not found. The requested URL /mvc/index was not found on this server.
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
in my root mvc there is only a few subdirs, .htaccess and index.php
To remove .php extension use following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
For .html extension Rewrite Rule will be
RewriteRule ^([^\.]+)$ $1.html [NC,L]

URL rewriting: conflicting rewrite rules

I'm trying to do three things with my URL rewriting:
remove .php file extensions
have my post URLs look like this: mydomain.co.uk/resources/example-post The default is mydomain.co.uk/resources/post.php?s=example-post
have my category archive URLS look like this: mydomain.co.uk/resources/category/example-category The default is mydomain.co.uk/resources/archive.php?cat=example-category
This is what I have in my .htaccess file at the moment:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
RewriteRule ^resources/([a-zA-Z0-9-/]+)$ /resources/post.php?s=$1 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /resources/archive.php?cat=$1&page=$2 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)$ /resources/archive.php?cat=$1 [L]
It's doing the first two things. But not the third.
If I remove
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
Then it does the third things, but not the first two.
Any ideas what's going? How do I get it do all three things at once?
Found a solution.
Just needed to swap the order around, I now have this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [L,QSA]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /resources/archive.php?cat=$1&page=$2 [L]
RewriteRule ^resources/category/([a-zA-Z0-9-/]+)$ /resources/archive.php?cat=$1 [L]
RewriteRule ^resources/([a-zA-Z0-9-/]+)$ /resources/post.php?s=$1 [L]
It's working fine.