Here is my code for .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*) http://localhost/h [R=301,QSA,NC,L]
I tried to execute multiple rules, but for the first one it works correctly(without index.php) and second one (removing www.) executes with extra "?" mark.
My URL(without www.) is: http://localhost/h/home/sub_cat_service/8 and
Resultant URL(adding with www.) is: http://localhost/h/?home/sub_cat_service/8
How i shall remove "?" mark from the URL?
thanks
Related
I have:
RewriteCond %{HTTP_HOST} ^my.domain.com$ [NC]
RewriteRule ^(.*)$ index.php?q=search&keyword=$1
Input:
my.domain.com/foo_bar
I want:
index.php?q=search&keyword=foo_bar
But in fact:
index.php?q=search&keyword=index.php
I don't understand why. Please help me!
Your rewrite rule is actually rewriting twice, once for /foo_bar and second time for index.php as .* matches anything.
You just need to add 2 conditions to stop rewrite for files and directories:
# handle landing page
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
RewriteRule ^/?$ index.php?q=search [L,QSA]
# handle /foo_bar
RewriteCond %{HTTP_HOST} ^my\.domain\.com$ [NC]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?q=search&keyword=$1 [L,QSA]
I'm trying to get rid of some subdirectories on my website.
I tried this link Remove two subdirectories from url with mod_rewrite but it does not work with my case.
Actually i have this structure : www.mywebsite.com/fr/news/index.html and I want it to look like www.mywebsite.com/index.html.
In my root directory a have a htaccess file with the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+fr/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^fr/)^(.*)/$ /fr/$1 [L,NC]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]
What would the change to do to make it work ?
Thank you in advance for your answers
I have this structure:
www.mywebsite.com/fr/news/index.html
and I want it to look like
www.mywebsite.com/index.html
Without knowing more, I'd suggest placing this in your root folder:
RewriteEngine on
RewriteRule ^[^\/]+\/[^\/]+\/(.*) http://www.mywebsite.com/$1
The regex reads as:
from the start, find one or more characters that aren't a forward slash
then a forward slash
then one or more characters that aren't a forward slash
then a forward slash
then capture all the remaining characters
I want to redirect my website to admin panel
Like
localhost/website/ => localhost/website/admin
localhost/website/login => localhost/website/admin
localhost/website/blog => localhost/website/admin
Here is my created rule
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /website/admin/$1 [L]
But this is not working
The following code will redirect everything to your admin page, keeping the attributes. This also applies to files who already exist !
RewriteEngine On
RewriteCond %{REQUEST_URI} !^website/admin
#RewriteCond %{REQUEST_FILENAME} !-f #OPTIONAL: Remove '#' if you want existing files to display normally
#RewriteCond %{REQUEST_FILENAME} !-d #OPTIONAL: Remove '#' if you want existing files to display normally
RewriteRule ^(.*)$ /website/admin/ [L,QSA]
Explanation:
turn on the rewrite engine:
RewriteEngine On
specific condition for rewriting: uri (the part after your domain name/localhost) cannot (! = inverse, NOT) start (^ = start of line) with website/admin = where you are rewriting to. This must be done to prevent looping.
RewriteCond %{REQUEST_URI} !^website/admin
Following 2 lines can be uncommented (remove the #) if you want that existing files are not rewritten
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
The rewrite himself:
^ = start of line
() = a substring
.* = a number of arbitrary symbols, or no symbols
$ = end of line
/website/admin = the place to rewrite to
[L,QSA] = extra options:
L=last: stop executing further lines
QSA=Query String Append: keep arguments, e.g. ?page=contact e.g.
RewriteRule ^(.*)$ /website/admin/ [L,QSA]
I hope this clarifies my solution.
Try:
RewriteEngine On
RewriteCond $1 !^website/admin
RewriteRule ^(.*)$ /website/admin [L]
You can try this with some updated as per your code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/index\.php/component/quates [NC]
RewriteRule .* index.php/component/quotes [R=301,L]
Or Read this link:
http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(website/admin)$ [NC]
RewriteRule ^(.*)$ website/admin [R=301,L]
Explanation:
First we make sure that the request is not for a file or directory that exists:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Then we see if website/admin is exactly our requested URL (case-insensitive [NC]):
^ indicates beginning, and $ end of expression.
RewriteCond %{REQUEST_URI} !^(website/admin)$ [NC]
If all conditions are met we redirect to website/admin with HTTP status code 301 (last step [L]):
RewriteRule ^(.*)$ website/admin [R=301,L]
Try this:
RewriteEngine On
RewriteRule ^website/(?!admin).*$ website/admin [L]
RewriteRule ^website/admin[^/]+.*$ website/admin [L]
We have a rewrite rule that looks like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
Which works as expected; if the file does not exist, forward to page.php which is a generic page and allows for pages with custom URLs.
We also want to force the www. to precede the domain: example.com -> www.example.com
For that, we use:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This also works, but when the two are supposed to work together:
example.com/non-existent-file
We end up getting:
www.example.bom/page.php
Which defaults to our 404 page. How do I modify these rules to work together? I have tried playing with some of the different suffic characters (ie. removing the L in the [NC,L] in an attempt to get both rules to process).
I have also tried repeating the forward to page.php lines beneath the www redirect (and removing the L suffix) with no success.
The entire HTACCESS file looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=302,L]
Also, is there a %{CONSTANT} I can use in place of example.com that represents the current domain?
The constant you're looking for is %{HTTP_HOST} as provided by the HTTP request. Perhaps you are looking for the variable %{SERVER_NAME} for the virtual host's default servername?
Place the hostname checking rule first, and use a RewriteCond in the page.php rule to only apply if the domain is already correct:
RewriteEngine On
# First rewrite the domain
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Then rewrite the files
# At this point, the domain should already have been enforced
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
I'm trying to change the word "portfolio" in my URLs to "portfolio/project" and inadvertently created a redirect loop. Would appreciate any help in pointing me in the right direction.
Example:
http://www.example.com/portfolio/interactive/abc/ to
http://www.example.com/portfolio/project/interactive/abc/
Current htaccess (last two lines relates to issue):
redirect 301 "/sitemap.xml" http://www.example.com/sitemap.php
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sitemap.php
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]
Your problem is that your regex also matches your target, so after the redirect, the URI matches the same rule and gets redirected again (you may have noticed that there's a bunch of /project/project/project/project/project/project/project in the URI)
Add an exclusion condition:
RewriteCond %{REQUEST_URI} !portfolio/project
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]