htaccess rewrite url from different pages - php

i make this in htaccess file .. for rewrite url from different page
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
and it's works fine ..
now i need to add more as facebook
you can put facebook.com/username
which is : facebook.com/profile.php?username=
& you can put facebook.com/pagename
which is : facebook.com/pages.php?username=
and works fine
from different pages ..
I need to make like this ..
any help ?!
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1 [QSA]

Your regex patterns match identically, so you're going to need something to differentiate between pages and people (or redirection to profile.php vs. index.php).
You can accomplish this one of two ways. First, you can go all to one page, let's call it redirect.php. On this page, you could check the id value and then redirect. So like:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ redirect.php?id=$1
In redirect.php, you grab $_GET['id'] and run some query against it to determine if it's a page or person, and then redirect using something like:
header("Location: profile.php?username=".$_GET['id']);
If you strictly looking for Facebook related stuff, you can hit the graph API up instead of the database to determine if its a page or person ID. (Not sure if it can exist in both - but I think not).
If you don't have a database or data source to determine if its a page or person, then you'll need to add something to your URL strings to determine, like /pages/{pageid}, which you could then rewrite your rules to:
RewriteRule pages/([a-zA-Z0-9_-]+)/?$ index.php?username=$1 # for pages
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?username=$1 # for people
If you can't use something in your URL and don't have a way to access it via database, then you're out of luck, as you're hitting two identical patterns.

Related

How to get the previous url in rewrite engine?

RewriteEngine On
RewriteRule ^(register)/?$
RewriteRule ^([\w-]+)/?$ profile.php?username=$1
On the second line, I would not like to intercept or redirect the user. If the url matches the given regex pattern, I would like to perform no operation and the url user requested should go where it was intended to. How do I do this?
Since you want to redirect all requests to profile.php except for register, you can add an additional rewrite condition to only redirect if request URI is not register.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/register/?$ [NC]
RewriteRule ^([\w-]+)/?$ profile.php?username=$1
Demo: https://htaccess.madewithlove.be?share=5f7d67b4-a374-5195-975e-a9742a685fdf

.htaccess keep multiple GET params

Part 1 takes care of making example.com/fr behave like example.com?lang=fr, or example.com/fr/some-page.php like example.com/some.page.php?lang=fr etc.
Part 2, which I'm currently working on not working well yet, is to obtain a new GET param for other pages called page, in this case if there's login in the url.
Problem: It seems like part of the page loads twice when going to for example example.com/login or example.com/fr/login.
Maybe un-necessary details here but for instance it says Facebook Pixel Error: Duplicate Pixel ID:, and similar errors for other tags I use like Mixpanel, and then my JS just stops working. That's all I can say about the problems I see on my side. Best chance seems to be about looking for flagrant errors in the htaccess rules.
What should be fixed in the rules so the end goal of having the GET param page and lang work fine?
RewriteEngine On
# Part 1
RewriteCond %{THE_REQUEST} \s/+[^?]*\?lang=([^\s&]+)\s [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_URI} !^/js/
RewriteRule ^([a-z]{2})(?:/([^/]+))?$ $2?lang=$1 [NC,L,QSA]
# Part 2 (this is the part I am adding, which isn't fully working well yet)
# anything looking flagrantly wrong? If for example we are on `example.com/fr/login`,
# according to rules in this htaccess file we should have 2 GET params, `lang` and `page`.
RewriteCond %{REQUEST_URI} login
RewriteRule .* index.php?page=login
# adding more pages the same way
RewriteCond %{REQUEST_URI} signup
RewriteRule .* index.php?page=signup
You can use the following rule as your Part1
RewriteEngine on
RewriteRule ^(en|fr)/login/?$ /index.php?page=login [L]
RewriteCond %{THE_REQUEST} \?lang=([^&]+)\sHTTP
RewriteRule ^ /%1%{REQUEST_URI}? [L,R]
RewriteRule ^(en|fr)/?(.*\.php)?$ /$2?lang=$1 [L]

How exactly does mod_rewrite work when displaying old url

I have used mod_rewrite to change http://mywebsite.com/?page=2 to http://mywebsite.com/page/2 using the following code:
RewriteEngine On
RewriteRule ^page/([^/]*)\.php$ /?page=$1 [L]
But when i type in the old url - http://mywebsite.com/?page=2 the page still appears and the url doesnt change to the static one. Is mod-rewrite meant to redirect the user or just make everything on the dynamic url appear on the static one when entered? If so how can i redirect any user that goes to the old dynamic url - http://mywebsite.com/?page=2 to the new static one http://mywebsite.com/page/2? Plus does google still index the old url?
This is my complete .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# external redirect from actual URL to pretty one
RewriteCond \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
Have your full .htaccess like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
mod_rewrite necessarily will not redirect the visitor to the new fancy URL, but it will make sure that anybody who visits the new URL will still access it as if it is using the old URL
I believe you can achieve something like that with mod_substitute, but that's not something I will advice you to do now.
If you can, manually change the links to reflect the new changes, or have a function which will parse out your links such as
http://mywebsite.com/?page=2 to http://mywebsite.com/page/2
such as
function fancyuri($url){
//blah blah, strip out ? and change all occurence of equality sign = to forward slash
//do some other tricks
//return formatted links
}
And then pass your links as a param to the function and have it return your nice urls, in this case, even if you don't want it again, you can strip off the formatting codes in the function and still have ur original url returned.

Making a clean URL with php arguments in it

So far, I have made my .htaccess to let me remove the ".php" extension, but that isn't enough for me. I want to be so that example.com/test?id=asdfjK could be able to be accessed as example.com/asdfjK. So that it accepts only the main php get argument in the URL (I don't know what to call them.
Here is my .htaccess file so far:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)/$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)$ /image.php?ID=$1
RewriteRule ^([a-zA-Z0-9_]+)/$ /image.php?ID=$1
There's no way to differentiate between what gets sent to index.php and what gets sent to image.php. The patterns are identical, which means everything will match the first one and nothing will get routed to image.php. You've got to add something to the url so that you can match against it. Something like:
http://example.com/image/abcdefg123456
And that means the htaccess file would look something like:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [L,QSA]
RewriteRule ^image/([a-zA-Z0-9]+)/?$ image.php?page=$1 [L,QSA]
The PHP arguments after the question mark are called the "query string".
Try something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?page=$1 [L]
This should redirect anything from:
http://example.com/abc123
To:
http://example.com/index.php?page=abc123
See also:
htaccess rewrite for query string
.htaccess rewrite query string as path
How to prevent infinite redirect loop on htaccess?

mod_rewrite for PHP's "lang?=" for more than one page?

I currently use the follwoing code in my .htaccess file
RewriteEngine On
RewriteBase /
# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array):
For instance:
example/en redirects me to example/?lang=en while keeping example/en in the URL.
But I also have a thanks.php page and the code above clearly just work for the index.php page.
How can I make the rewrite rule to work for both index.php and thanks.php page?
The most straight-forward way is just to do this:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]
If you want to make it more general, you have the option of white-listing files, like this:
RewriteCond $1 ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]
...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]

Categories