I am trying to set up my htaccess file to perform these redirections:
http://www.mysite.com/about should link to http://www.mysite.com/content/pages/about.php
http://www.mysite.com/login should link to http://www.mysite.com/content/pages/login.php
http://www.mysite.com/prices should link to http://www.mysite.com/content/pages/prices.php
Thanks
To only redirect URLs you have provided you can use this kind of rule (you can add other pages to the list of you need):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(about|login|prices)$ /content/pages/$1.php [L]
To redirect ALL non-existing pages to /content/pages/PAGE_NAME.php, you can use this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# do not do anything for already existing files (like images/css/js etc)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# redirect everything else
RewriteCond %{REQUEST_URI} !^/content/pages/
RewriteRule ^(.+)$ /content/pages/$1.php [L]
NOTES:
You need to place these rules into .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
Related
I have a custom application that needs to redirect all requests through a central router, and I need to add an exception for LetsEncrypt to make requests to the temporary /.well-known directory created directly beneath the document root.
Say my original .htaccess file is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^.*$ router.php
I modified it to read:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/.well-known.*$
RewriteRule ^.*$ router.php
But I can see requests still being forced into the router. Why doesn't this rewrite condition work?
You may use this rule:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/\.well-known/?[?\s] [NC]
RewriteRule ^ router.php [L]
Using additional RewriteCond to skip files and directories and using THE_REQUEST instead of REQUEST_URI.
This is my .htaccess code for clean url rewriting.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[a-zA-Z]{3,}\s/+index\.php\?bank=([^\s&]+) [NC]
RewriteRule ^ %1%2%3%4? [R=301,L]
RewriteRule ^([^/]+)/?$ index.php?bank=$1$2$3$4 [L,QSA]
This code works fine for single parameter in url. For example it rewrites
http://example.com/example-path/ to
http://example.com/index.php?bank=example-path/
But when I tried to pass multiple parameters in url, all I got are errors.
I need a url like
http://example.com/example-path instead of http://example.com/index.php?bank=example-path
How can I alter My code to pass multiple urls.
Your redirect logic can be greatly simplified to just:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#If a request does not match an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
#And does not match an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite all requests to index.php
RewriteRule ^(.*)/?$ index.php?bank=$1 [L,QSA]
I need to show content of wildcard url to its particular subfolder ..
For Example
Right now http://youbridge.info/watchvideosonline/random-hello.html is showing content of home page , instead i need to show contents of that particular subfolder/index.php (i.e content of http://youbridge.info/watchvideosonline/)
I tried below code , but its not working
RewriteRule ^([^/]+)/?$ index.php?filter=$1 [QSA,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/.*$ /$1/index.php [L]
Currently I have these rules in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).css style.php?u=$1 [QSA]
RewriteRule ^(.*).xml rss.php?u=$1 [QSA]
</IfModule>
This will rewrite the following URLs:
http://domain.com/user.css
http://domain.com/user.xml
But when I'm trying to grab a file from a subdirectory: http://domain.com/css/style.css it gets rewritten as well.
My goal is rewrite only for current directory and avoid sub-directories, since all real CSS files on sub-directories will be rewritten.
How I can avoid this?
You need to make your pattern more restrictive: this ^(.*).css will match ANYTHING with .css in it while this pattern ^([^/]+)\.css$ will be restricted to something.css (styles\something.css will not match it).
<IfModule mod_rewrite.c>
RewriteEngine On
# do not do anything to real files or folders
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
RewriteRule ^([^/]+)\.css$ style.php?u=$1 [QSA,L]
RewriteRule ^([^/]+)\.xml$ rss.php?u=$1 [QSA,L]
</IfModule>
The easiest way is to tell mod_rewrite to avoid rewriting if the file is a real file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).css style.php?u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*).xml rss.php?u=$1 [L,QSA]
I've added a [L] tag (end) because once a rule is applied you certainly doesn't need the next rule to be checked.
Now if the files really exists but you really want to handle them with a php script if no 'css' subdirectory is present on the url... let's try that:
<Location "/css">
RewriteEngine off
</Location>
I need to allow urls like http://example.com/forum on my website.
Here is my .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z-]+)$ /$1.php [L]
Currently, I get a 404 error.
If I change ^([a-z-]+)$ /$1.php [L] to ^([a-z-]+).html$ /$1.php [L], and my url http://example.com/forum.html, it works.
I want something like stackoverflow, http://stackoverflow.com/questions
EDIT: It works with WAMP in localhost, but on the website, it doesn't.
What's missing?
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.+) /$1.php [L,QSA]
I have added Options -MultiViews -- can make a big difference on some configurations (possibly your case).
Rule is now also checking if such .php file actually exist. If not -- no rewrite occurs.