I'm trying to point all URLs to index.php and having a url parameter that gives me the full URL.
This is my rule:
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]
It works for example with this URL:
mywebsite.com/hello
$_GET['url'] //hello
But with this:
mywebsite.com/hello/bye
$_GET['url'] //hello
The bye doesn't appear.
Related
I am struggling a lot trying to redirect API calls to a different index file.
I want to redirect all URL's that have [domain-name]/API/* in the URL to a different script: API_index.php.
I tried doing it with the following in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} /API/
RewriteRule . API_index.php [L]
RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/) - [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [NC,L]
According to this website this URL, http://abayocms.dev/API/test, should match, but I am still being navigated to index.php?
ANSWER
Apparently the .htaccess [L] only stops the current iteration over the URL.
Once I redirected to the API_index file with my first rewriteCond + Rule, the server starts a new iteration with the new URL, which would be [some domain]/API_index.php.
This gets catched by my last rule and still gets redirected to index.php
Thus this scenario needs its own rule:
RewriteRule ^API_index.php$ - [L]
The total file looks like:
RewriteEngine on
# Prevents redirecting API_index.php to index.php
RewriteRule ^API_index.php$ - [L]
RewriteCond %{REQUEST_URI} /API/
RewriteRule . API_index.php [L]
RewriteRule ^(font-awesome|fonts|ajax|tools|log|documentation)($|/) - [L]
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav)$ index.php [NC,L]
Big thanks to #starkeen for the answer!
It's because /API_index.php is matched by your 3rd rule and gets rewritten to /index.php .
You need to pass the /API_index.php untouched, try adding the following after RewriteEngine on
RewriteRule ^API_index.php$ - [L]
I'm looking for a .htaccess Rewrite URL solution to convert my web application's ugly URL to Pretty one.Previously I have converted an URL localhost/example/user.php?u=username to localhost/example/username with the following .htaccess code
RewriteRule ^([^/\.]+)/?$ user.php?u=$1
now I want to convert the following URL:
localhost/example/messages.php?u=username ----> localhost/example/messages
Thanks.
Keep these 2 rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^([^/.]+)/?$ user.php?u=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/.]+)/?$ /$1/user.php?u=$2 [L,QSA]
I've this URL:
http://www.domain.com/search/madonna
and this condition runs properly:
RewriteRule ^search/(.+)?$ /index.php?s=$1 [QSA,L]
I want to use this URL:
http://www.domain.com/search/madonna.html
But this condition doesn't work and I get a 404 error:
RewriteRule ^search/(.*)\.html$ index.php?s=$1 [QSA,L]
Any help?
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've wrote .htaccess file to convert urls to SEO friendly :
the original url is :
http://palestinianz.com/?page=person&p=Alex-Atalla
the content of .htaccess is :
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
it should produce a link like this :
http://palestinianz.com/Alex-Atalla.html
But it makes no effect although I put the file in the root of my website !
where is the problem ?
If you want the URL to change in the address bar, you need to redirect the browser, then rewrite on the server. The first part of this answer explains the process that you want: https://stackoverflow.com/a/11711948/851273
So looking at the second list, the process is to redirect the browser if an ugly link is request to a nice looking link:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?page=person&p=([^\ ]+)
RewriteRule ^$ /%1.html? [R=301,L]
Now, on the server end you need to internall rewrite it back to the ugly url, which is more or less what you already have:
RewriteRule ^([^/]*)\.html$ /?page=person&p=$1 [L]
You can try:
RewriteCond %{REQUEST_URI} (.+)\.html$
RewriteRule ^(.+)\.html$ ./index.php?page=person&p=$1 [L]
Only problem with this is any .html page will go through this. May want to change to the following:
# URL: domain.com/person/Alex-Atalla.html
RewriteCond %{REQUEST_URI} (.+)/(.+)\.html$
RewriteRule ^(.+)/(.+)\.html$ ./index.php?page=$1&p=$2 [L]
This will allow you to have more robustness in changing the the page variable as well