mod_rewrite for URL with two URL-parameters - php

I want to be able to open the following url
http://example.com/login/12345
and in the background, it should load:
http://example.com/index.php?endpoint=login&access_key=12345
Obviously, I have tried many htaccess generators to create an appropriate RewriteRule set.
For example the following (from: http://www.generateit.net/mod-rewrite/index.php):
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
Even though I know for a fact that .htaccess and mod_rewrite is enabled on my server (i tried to setup a test to redirect all requests from example.com to example2.com using htaccess and mod_rewrite, and it worked), I can't get this to work.
I have now spent nearly 2 hours to find for a solution on stackoverflow and other websites, but all my attempts failed.
I hope someone can help me find out the correct way to rewrite my url in the above example.like explained above.
Thanks a lot in advance!

Try with this:
RewriteEngine On
RewriteRule ^([^\/]*)/([^\/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
#-Added-these---^--------^
When tested, here is the result:

Add this to the .htaccess file in your DOCUMENT_ROOT
Assuming login is not variable
RewriteEngine On
RewriteRule ^login/(\d+)$ index.php?endpoint=login&access_key=$1 [L,NC,DPI]
** If login is variable**
This is in case you want to redirect not only for login, but also for urls such as http://example.com/housenumber/12345
RewriteEngine On
RewriteRule ^([^/]+)/(\d+)$ index.php?endpoint=$1&access_key=$2 [L,NC,DPI]
Tested on actual Apache 2.2 and 2.4 :)

Related

Problem with url-rewriting for a multi-language website

I have tried to set somes url rewriting rules for my multi-langage website.
It was working, and I wanted to apply some corrections, and now it is not working anymore.
When I tried this url : http://mywebsite.fr/fr/ , the browser changes the url for http://mywebsite.fr/fr/?lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr
Here is the code:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(fr|en|nl)/$ index.php?lang=$1&%{QUERY_STRING} [L]
I said to myself that kind of bug couldn't come from url-writing, so in my php code I have put an "return false" as the very beginning of the page, the problem still occurs whit a white page...
Also, if I disable all the url-rewriting rules, I recevied an apache error "Not found"...
I have also tried to restart Apache, same problem...
Anybody have an idea ?
Thanks !
What you see is a typical rewrite loop. The cause is that you unconditionally rewrite, regardless whether the goal of rewriting has already been achieved, so whether rewriting has already been performed before.
You can get around that using a condition:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} !^lang=
RewriteRule ^/?(fr|en|nl)/?$ /index.php?lang=$1 [L,QSA]

Rewrite causing 404

I am trying to do a simple URL rewrite and I have searched apache.org and SO for an answer but have come up short. I executed the request and watched with a network utility and the server is throwing a 404. I am really sorry to be posting this simple question but I really don't see why this rule is not working. Thanks
I want to have this SEO-friendly url format
/products/my-product
Go to this physical file in the file system
/my-product.php
BTW: The folder "products" does not exist; it's just there for SEO reasons
Here is my .htaccess (Removing "RewriteBase /") has no effect
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
RewriteRule ^products/([A-Za-z\-]+)/?$ $1.php [L,QSA,NC]

How i know .htaccess file is working or not?

I use this htaccess url
mywebsite.com/xyz/search.html
here xyz is a folder in root
in .htaccess is use the code for this url
# enable apache modRewrite module #
RewriteEngine On
RewriteBase /
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [L,QSA]
now i want this
xyz/search.html is hit the
url xyz/index.php?page=search
but this:
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [L,QSA])
code is not working..
any idea regarding this...
Type some random characters in your .htaccess file and try to reload your page, if you see error 500 then your .htaccess file is working, put your random characters after "RewriteEngine On" .
Not sure that rule would work. Does this one do the job?
RewriteRule ^.*/(.*)\.html?$ index.php?page=$1 [L,QSA]
Also, if you want to test the rule with a bit more visibility, you can add R=302 to the flags, that way your browser will get a redirect and you'll be able to see the rewritten URL in the address bar
I wonder why people don't use RewriteLog.
Put in the same place:
RewriteLog /tmp/rewrite.log
RewriteLogLevel 3
It slows down the server but for debugging it's made for.
Try:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/?([^/]+)\.ht(m|ml)?$ $1/index.php?page=$2 [L,QSA]
There is a very handy online tool for testing htaccess files. Simply paste your redicrect rules in the form provided and test various urls to see if they are redirected or left untouched.... soooper easy!

Mod Rewrite in PHP

This is my Blog page's url
www.mysite.com/blog/?id=1
www.mysite.com/blog/?id=2
www.mysite.com/blog/?id=3
But I want to look my url look like
www.mysite.com/blog/1
www.mysite.com/blog/2
www.mysite.com/blog/3
I used the following Rewriting in the .htaccess.
But I am getting Error 404 from my System.(Though the subdirectory /blog is there)
What is the mistake done by me and How can i fix this ?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog/?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog/?id=$1
Firstly check mod_rewrite is enabled on apache:
Then add the following to your .htaccess file
RewriteEngine On
RewriteRule ^/?blog/([a-zA-Z0-9_-]+)/$ /blog?id=$1 [L]
Also check AllowOverride All is given in the directory for this module in apache.
Following link can be useful:
AllowOverride for .htaccess on local machine giving 403 Forbidden
I spent almost two days last year chasing this one!
Many Linux distroes (at least Debian-based) prevent you from using a .htaccess file to override the Apache configuration files by stating AllowOverride None in the default config file.
You can change the line to AllowOverride All by editing /etc/apache2/sites-available/000-default
Hope this helps.
This is called Pretty URL's or SEO Friendly URLs, this can be achieved by several ways.
One way is doing everything yourself and modifying the .htaccess to look something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Note that this requires your webserver to be Apache.
Other ways (which actually work somewhat the same) is by using PHP Frameworks which often have Routing with Pretty URL's implemented so you don't have to reinvent the wheel.
This is Laravel's approach of creating routes and pretty url's:
Route::get('/blog/{id}', function() {
// Do something
});
Take a look at Laravel: http://www.laravel.com. I really love this framework and its features.
Ruby on Rails has this implemented by default as well. But I assume you are not using it since you are programming PHP.
For a good tutorial explaining how to create Pretty URL's within PHP yourself take a look at this tutorial from TutsPlus: http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
Hope I helped you! Good luck.
I am not good in english, but here i can try to answer.
look like your blog is on a sub dir.
so,
RewriteRule ^blog/([^/]*)$ /blog/?id=$1 [L]
and try to add
RewriteBase /
if you still have problem,
also..
there are many generator for htaccess
for example
http://www.generateit.net/mod-rewrite/index.php

RewriteRule .htaccess multiple params

I'm trying to rewrite my url, but I'm getting an error "500 Internal Server Error"
I've never done this before, and I've read some tutorials but I can't say I got any smarter from it.
The .htaccess file is in the root directory (/var/www)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /sub_domain/directory_to_my_files/
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*)/$ /index.php?pos=$1:$2&text=$3
The current link goes like this:
http://sub_domain.my_domain.com/directory_to_my_files/index.php?pos=(float|integer-only):(float|integer-only)&text=(any-text)
But I'm trying to do this:
http://sub_domain.my_domain.com/directory_to_my_files/(float|integer-only):(float|integer-only)/(any-text)/
Sorry if the links is a bit hard to read.
This should be placed in /directory_to_my_files/.htaccess
RewriteEngine On
RewriteBase /directory_to_my_files/
RewriteRule ^(-?[0-9.]+)[:-]([0-9.]+)/([^/]+)/?$ index.php?pos=$1:$2&text=$3 [L,QSA,NE]
I've tested this locally:
( I did not need the rewrite base )
RewriteEngine on
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*) /index.php?pos=$1:$2&text=$3 [R,L]
And it has redirected me
from
http://loc.domain/555-666/misc-text
to
http://loc.domain/index.php?pos=555:666&text=misc-text
I reckon this was what you wanted?
About the [R,L] at the end:
the R is telling apache to actually redirect, not just rewrite the url - this helps with testing.
the L is saying this is the last rule
Edit: could it be that the rewriteBase is causing you problems? Try it without and see if it works - at least you can nail where the problem is, then.

Categories