htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^member member.html [NC,L]
RewriteRule member/(.*)/(.*)$ member.html?a=$1&b=$2
so the should be like https://example.com/member/query_1/query_2
the problem is this link wont work unless I add (s) character to the end of member word in the url
so the working link is https://example.com/members/query_1/query_2
unless it return me 404 error
its work fine in the xamp localhost but when I upload to server I have this issue.
You may use this code in site root .htaccess:
Options -MultiViews
RewriteEngine On
RewriteRule ^member/?$ member.html [NC,L]
RewriteRule member/([^/]+)/([^/]+)/?$ member.html?a=$1&b=$2 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
Options -MultiViews will turn content negotiation service off. Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
Related
This rule takes us to the error page
RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]
I have the following in my .htaccess file
<IfModule mod_rewrite.c>
RewriteRule ^sucuri-(.*)\.php$ - [L]
</IfModule>
# END - Allow Sucuri Services
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ErrorDocument 404 /404.php
Options +FollowSymLinks
Options +MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www.xxxxx.com$ [NC]
RewriteRule ^(.*)$ https://www.xxxxx.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ci_index.php?/$1 [L]
## Remove php extension
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]```
With the following rule
```RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]```
Trying to achieve the following -
```https://www.xxxxx.com/latest?auth=US-mobile-county
to
https://www.xxxxx.com/latest/US-mobile-county```
This rule takes us to the error page RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]
You've not stated precisely what "error page" you are referring to? Or what is expected to handle this request. This directive is not correct by itself, so it's not immediately clear what it is you are trying to do. I'm assuming the intention is to rewrite to latest.php (not latest as suggested by this rule, and mentioned later in the question) - since this would seem to be the only reason to implement such a rule (and your question is tagged php). By rewriting to latest only you are dependent on other directives appending the .php extension - and therein lies a conflict.
There are a number of issues with the directives as posted that is preventing this from working. Notably, the rules are in the wrong order and the use of MultiViews (probably in an attempt to get extensionless URLs working) is compounding matters. In fact, it doesn't look like the rule in question is actually being processed at all.
Without MultiViews, and due to the order of the directives, a request of the form /latest/something would be rewritten to /ci_index.php?/latest/something (presumably a CodeIgniter front-controller) which I would guess would result in a CI generated 404 response. However, since MultiViews has been enabled mod_negotiation first "rewrites" the request to /latest.php/something, which doesn't match any of your rules so either results in a 404 (depending on your server config) or calls latest.php but without any URL parameter, which presumably causes your script to fail?
https://www.xxxxx.com/latest/US-mobile-county
Also, note that your example URL contains hyphens (-), but the regex in your directive (ie. ^latest/([A-Za-z0-9]+)$) does not permit hyphens so it wouldn't have matched anyway.
Try the following instead, replacing everything after the ErrorDocument directive:
# Disable MultiViews
Options +FollowSymLinks -MultiViews
RewriteEngine on
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
# Rewrite "/latest/something" to "/latest.php?auth=something"
RewriteRule ^latest/([A-Za-z0-9-]+)$ latest.php?auth=$1 [L]
# Allow extensionless PHP URLs to work
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
# Front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ci_index.php?/$1 [L]
Note that I've reversed the order of the directives so that the rule in question is now first and the CI front-controller is now last. The order of the directives in .htaccess is important.
Since you had enabled MultiViews (now disabled in the above), your rule to enable PHP extensionless URLs (that you had labelled "Remove php extension") was not actually being used at all (unless you had directories or files that contained dots, other than that used to delimit the file extension).
I have a problem with a simple rewrite rule in htaccess ...
My htaccess like this :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^company$ /company.php?lang=en [QSA]
RewriteRule ^company/about$ /article.php?lang=en [QSA]
In local, it works.
But online it doesn't work.
If I go to the URL "www....com/company/about", I have the company.php page ...
Can you explain to me what's my problem ?
Thanks a lot
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteBase /
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
RewriteRule ^company$ company.php?lang=en [L,NC,QSA]
RewriteRule ^company/about$ article.php?lang=en [L,QSA,NC]
Important change is disabling option MultiViews. Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.php (if that exists in your system).
I have also added L (last) and NC (ignore case) flags in your 2 rules.
On a CentOS 6 running apache 2.4 and DirectAdmin, I have a wordpress website. Beside the wordpress standard architecture, I want to have some semi-static pages which are located in a sub-directory.
Filesystem is as: /home/user/public_html/sub/static1.php
Desired URL is as: https://domain/sub/static1
currently I have no problem opening the pages with .php extension. But I want to remove it. I have tried adding some rewrite rule in .htaccess but I have failed since the request gets redirected to homepage.
I have tried to find some solution online, including this website but nothing could help me. I assume DirectAdmin is involved in this issue.
Any help would be appreciated
In your /home/user/public_html/sub/.htaccess file, use this code:
Options -MultiViews
RewriteEngine on
# remove php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# rewrite with php php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/sub/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can use Apache mod_rewrite
Just add this code to your root directory .htaccess
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
I was trying to rewrite a URL for making my site SEO friendly, but .htaccess rewrite not seems to work.
My URL is
www.tasteofkochi.com/dine-detail.php?a=150
I need this to look like
www.tasteofkochi.com/sometext/150
I did the simple formula but it's not reflecting, intact nothing happens at all. If I add some junk char in htaccess, site crashes, which means htaccess is working fine. Also I added a formula to remove .php extn, and that too works fine. Only issue is with this one. Can anyone please help me. I enable rewrite in httpd and allow all in directories, still not working.
Below is my .htacces
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
## hide .php extension
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteRule ^detail/([0-9]+)/?$ dine-detail.php?a=$1 [NC,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?p=$1 [L,QSA]
We can create pretty urls with .htaccess and php by mainly two files one is .htaccess and another index.php
Example
I am using a subdomain URL for my test server. Something like:
http://dev.mysite.com
I have the following in my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1
RewriteRule ^about/(.*)$ /about.php?request=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
The above works well on the production (non-subdomain) set of URLs. But it doesn't quite make the cut on my dev box. For example, if I type in
http://dev.mysite.com/about
I get a 404 error. But the following with the appended .php extension works.
http://dev.mysite.com/about.php
I am guessing this has something to do with the first rewrite condition that deals with the www subdomain. How would I modify my .htaccess file to account for the dev subdomain?
It appears that your .htaccess is not enabled.
Verify whether your .htaccess is enabled or not, by putting same garbage text on top of your .htaccess and see if it generates 500 (internal server) error or not?
To enable .htaccess your httpd.conf file will need this line:
AllowOverride All
You need at least AllowOverride All or AllowOverride FileInfo in order for the htaccess file to work in your server/vhost config. FileInfo is for things like mod_rewrite.
Additionally, make sure you have mod_rewrite loaded.
You'll also want to change this condition:
RewriteCond %{REQUEST_FILENAME}.php -f
to:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
because mod_rewrite will recognize that about has a php file and make it so /about/something passes the -f test, resulting in /about/something.php.