I want to mask the URL http://example.com/index.php?path=controller/function to http://example.com/controller/function. function isn't required to be present.
How can I do this with an htaccess file?
The following isn't working.
RewriteRule ^/?assets/(.*)$ assets/$1 [L] # directory for CSS, images, and JavaScript
RewriteRule ^$ /index [redirect]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z/]*)$ index.php?path=$1/$2 [L]
Currently, if I enter http://example.com/controller/function in the browser, I receive a 404 error, but entering http://example.com/index.php?path=controller/function works.
Thanks!
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)(.*)?$ index.php?path=$1$2 [L]
It maps internally
http://example.com/var1/var2
To:
http://example.com/index.php?path=var1/var2
Keeps the incoming trailing slash behavior and var2 is optional.
Related
I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
For example:
RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L]
RewriteRule ^admin/([a-z-])/([0-9a-z-#._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L]
I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error
The requested URL was not found on this server.
The full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^admin/add-news/?$ admin/add_news.php [L]
RewriteRule ^admin/edit-news/([0-9a-z-#._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L]
Files locations are:
1. C:\wamp64\www\test.com
2. C:\wamp64\www\test.com\admin\add_news.php
C:\wamp64\www\test.com\admin\edit_news.php
3. I want redirect from URLs with underscores to hyphens, for example:
from admin/add_news.php to admin/add-news
Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id
Example: admin/edit-news/my-news-title/2
Any ideas how to fix it? Thank you.
With your shown attempts, please try following htaccess rules file.
Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.
Samples specific rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L]
Generic rules:
RewriteEngine ON
RewriteBase /test.com/
##First rule for hitting url admin/add-news in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L]
##Second rule for hitting url admin/edit-news/name/id in browser.
RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L]
I want my WordPress based homepage to automatically start at abc.com/#about if the user enters abc.com.
I have added the following to my .htaccess file:
RewriteBase /
RewriteCond %{REQUEST_URI} !=about
RewriteRule ^$ /#about [L,R,NE]
The URL is changed, but the browser (chrome) give the error: “This webpage has a redirect loop”.
I get the same behavior if I use
RewriteCond %{REQUEST_URI} !=/#about
or
RewriteCond %{REQUEST_URI} !=#about
It could be because the URL contains a #, but I’m not sure.
Thanks in advance.
One solution is to use cookies. You can set a cookie after redirecting to URI with # and then delete it. Try:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} (^|\s)test=1($|\s)
RewriteRule $ - [L,CO=test:0:.your_site.com:-1]
RewriteRule $ /#about [L,R,NE,CO=test:1:.your_site.com]
Problem is that for the URL http://domain.com/#about browser doesn't send URI path after hash # to server and server will still see URL as http://domain.com/ which will make it redirect again and eventually browser shows looping error to you.
You can try this rule to stop the looping:
RewriteRule ^$ /index.html#about [L,NE,R]
OR else if index.php is your default file then:
RewriteRule ^$ /index.php#about [L,NE,R]
Try This:
RewriteBase /
RewriteCond %{REQUEST_URI} !=about
RewriteRule $ /%23about [L,R,NE]
The # part is replaced with URL encoded form %23. and the part which has to be rewritten should be $ , not ^$
I have a page with this settings:
Options All -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^(.*)show(/|.*)$
RewriteRule . /demo/index.php [L]
</IfModule>
For example, when you request the page http://example.com/demo/any_folder/ it loads http://example.com/demo/index.php without changing the URL, to retrieve data from a database for example. Well, this works.
But I want to add and exception.
I need a condition that if you visit http://example.com/demo/any_folder/show it loads the same URL removing the word "show" but avoiding the redirect that is actually defined. So, it will load http://example.com/demo/any_folder/ and not http://example.com/demo/index.php. Does anyone know how to do this?
There has to be something different about the URL to not be redirected to index.php. The following .htaccess code adds a query parameter ?show to tell the two URLs apart.
Since, the redirection is internal; the user never gets to see the parameter.
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)/show/?$ $1?show [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{QUERY_STRING} !^show [NC]
RewriteRule . /demo/index.php [L]
I would like to rewrite the English names of php files to their Dutch equivalents.
For example: someurl.com/news.php?readmore=4#comments should become someurl.com/nieuws.php?leesmeer=4#kommentaar. The code from news.php should be executed but nieuws.php should be in the url the arguments should function as well.
I tried several htaccess examples but I can't get it to work.
Any help would be appreciated.
Edit: Working progress from answers below and final solution.
RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?leesmeer=%1 [R=301,L]
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
RewriteRule ^nieuws.php$ news.php?norewrite [QSA]
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^search.php$ zoeken.php [R=301,L]
RewriteRule ^zoeken.php$ search.php?norewrite [QSA]
# make sure rewrite is activ
RewriteEngine On
# Rewrite a request for nieuws.php to news.php
RewriteRule ^nieuws.php$ news.php
Should do the trick.
Instad you could send all requests to an index.php and parse them there:
## Redirect everything to http://hostname/?path=requested/path
RewriteEngine On
RewriteRule ^([\w\W]*)$ index.php?path=$1 [QSA]
[QSA] makes sure you get the original get arguments too.
Now you have to parse the request in $_GET['path'] in you index.php and include the requested page.
eg:
if ($_GET['path'] == 'nieuws.php') {
include 'news.php';
} else if (empty($_GET['path'])) {
echo "HOME";
}
if you want to make make the user always sees nieuws.php in its address bar, even if he requested news.php, you could try the following:
RewriteEngine On
# Redirect news.php to nieuws.php if and only if the request comes from the client
# (suppose the client didn't set ?norewrite.)
RewriteCond %{QUERY_STRING} !^norewrite[\w\W]*$
RewriteRule ^news.php$ nieuws.php [R=301,L]
# Send news.php if nieuws.php was requested and prevent news.php from being redirected
# to back to nieuws.php by the rule above.
RewriteRule ^nieuws.php$ news.php?norewrite [L,QSA]
(R=301 means send a "moved permanently" redirect to the client, L means stop rewriting after this rule matched)
The hole thing with norewrite (you could use something else instead) is only needed to avoid an endles loop of rewriting between news and nieuws.
To translate the GET arguments, you can try the following code before the first line of the above code:
RewriteCond %{QUERY_STRING} ^readmore=(.*)$
RewriteRule ^news.php$ nieuws.php?lesseer=%1 [R=301,L]
Things after a the # in an url can't be changed in .htaccess, since they aren't send to the server at all. The only chance to change them is using JavaScript. (See lots of question here on manipulating them within JavaScript)
Try:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news\.php
RewriteRule ^ /nieuws.php [L,R=301,QSA]
RewriteRule ^nieuws\.php$ /news.php [L,QSA]
I'm looking to do a .htaccess file that let any requests pass to its original destination but redirects to a specific folder (a splash screen in this case) is no destination is specified. I'm not very well-versed with .htaccess and would appreciate some help.
Example: I'm requesting http://www.domain.com/folder/file.php, it should go through. But if I'm requesting http://www.domain.com/, it should redirect to http://www.domain.com/splash/.
What I have so far redirects correctly to /splash/, but redirects everything to /splash/.
<IfModule mod_rewrite.c>
RewriteEngine On
# If the requested URI is empty...
RewriteCond %{REQUEST_URI} !^$
# ...then redirect to the "splash" folder
RewriteRule .* splash [L]
# Otherwise rewrite the base
RewriteBase /
# If the request is not a folder or a file, redirects to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks!
EDIT: One important point would be to redirect http://www.domain.com/ to http://www.domain.com/splash/, but allow direct access to http://www.domain.com/index.php.
The %{REQUEST_URI} variable includes a leading slash, so it will NEVER be blank. You can get rid of that and just use this rule:
RewriteRule ^/?$ /splash/ [L,R]
If you want the URL that appears in the browser's address bar to stay http://www.domain.com/, then remove the ,R from the square brackets: [L].