.htaccess keep multiple GET params - php

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]

Related

mod_rewrite rule to handle base directory but second rule for any sub directories?

I'm trying to use mod_rewrite to simplify my website structure but it is proving difficult.
I need any exact matches of example.com/inventory/ to redirect to example.com/#search/ (I have this working).
I need to have any urls that match example.com/inventory/(.*) to load normally and not be redirected to /#search/ (I have this working).
I need to have example.com/#search/(.) redirect to example.com/inventory/$1 except in the following cases:
-example.com/#search/order-miles/
-example.com/#search/order-year/
-example.com/#search/order-price/ example.com/#search/page-(0-9)+/ -example.com/#search/makemodel-(.)/
Here is what I have so far:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^inventory/(.*)$
RewriteRule ^(.*)$ https://example.com/inventory/$1 [R,L]
RewriteCond %{REQUEST_URI} ^#search/(.*)$
RewriteCond %{REQUEST_URI} !^#search/(order-miles|order-year|order-price|makemodel)$
RewriteRule ^(.*)$ https://example.com/inventory/$1 [R,L]
RewriteCond %{REQUEST_URI} ^inventory$
RewriteRule ^(.*)$ https://example.com/#search/ [R,L]
It appears this is all working except for the second set of conditions and corresponding rule.
/#search/order-miles/ loads properly (and the other 3 subfolder structures), but the other urls, such as:
https://example.com/#search/nissan-frontier/9017/nissan-frontier/
should redirect to
https://example.com/inventory/nissan-frontier/9017/nissan-frontier/
and does not.
Does anyone see a better way to do this?

Preety Url: stop loading page with .php extension

I am using pretty url for my project and it is working fine.
http://testurl.com/user/12345
I am using .htaccess for redirection.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ user.php?id=$1&name=$2 [L]
Today i found if i change the link in browser like this http://testurl.com/user.php then page is also loading i want to show error message (Alert) if someone directly trying to access
The best way is to add your error or redirect in the user.php without id query string.
But you also can add (after RewriteBase /):
RewriteCond %{THE_REQUEST} \s/user\.php\s [NC]
RewriteRule ^ - [F]
You might as well solve your problem through code reorganization. I did this in one of my projects, and it has worked well.
1. When you create a pretty URL, move the according file into another directory
So, in this case, you had the URL example.com/user.php?id=123 visible externally. Now, you want a pretty URL for it, e.g. example.com/user/123.
On file level, before you had
- user.php
I suggest you move that to another directory, where all scripts live which are accessed by pretty URL only:
- rewrites/
|- user.php
2. Create redirects for your old URL to your new URL, externally
The same as you did above.
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
3. Rewrite the new, pretty URL to moved script, internally
The same as you did above, with difference that the directory name rewrites is added.
RewriteRule ^user/([0-9]+)/?$ rewrites/user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ rewrites/user.php?id=$1&name=$2 [L]
4. example.com/user.php now fails with a 404
Because /user.php does not exist anymore in the file system, it automatically fails with a 404 if called without params.
5. Benefits
This approach might sound like additional work for nothing, but these are the benefits making it worthwile in my opinion:
You do not need an additional .htaccess rule for error handling
You get better code organization
You get better overview of what is already accessible with pretty URL

htaccess RewriteRule - Removing '/' From The URL Causes Request To Display In URL

I have a little problem concerning .htaccess.
The problem lies more in aesthetics than functionality.
I have a very small htaccess file which contains the following, and nothing more:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9\/\-]*)$ index.php?request=$1 [QSA]
This sends any requests to index.php, parsing in the URI. I then manipulate/read the URI to get the relevant file.
An example URL would be this:
http://localhost/domain/fixtures/2015/
Now, if I was to remove the end '/', and hit enter, the URL would change to:
http://localhost/domain/fixtures/2015/?request=fixtures/2015
I have tried adding a '/' to the rewrite but it does nothing.
I have searched across Google to no avail. (Bearing in mind some of my searches went along the lines of, "/ added to url htaccess rewrite")
The main reason I want to get this sorted is because it doesn't look pretty, but also because it creates duplicate content (i.e. you can get to the same page from both URL), which is not good for SEO.
Any direction I can be pointed in is a great help.
Cheers
If I am reading this right, you want external URLs in this form:
http://localhost/domain/fixtures/2015/
And NOT this form:
http://localhost/domain/fixtures/2015
In order to achieve this, you need to use a redirect to let the remote
client know that the latter form is not valid, e.g.,
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
If you are also serving static content, you might want this as well:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
So, the full recipe including your PHP content generator:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([a-zA-Z0-9\/\-]*)$ index.php?request=$1 [QSA]

url rewrite and redirect - php

I recently developed a web site using php which accepts a id as its input via get.
http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html
So in order to archive this, i managed to do some research and end up with this code blow.
Also i'm passing the username in the query string.
http://website.com/profile.php?id=123&username=test_user
the parameter ID is only used as a input for the profile.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
The final output i seek is http://website.com/username.html
Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.
Try this rule:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
then you should be able to use
http://website.com/profile/123/username.html
as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.
If you only need the username, use
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
and the URL would be
http://website.com/profile/username.html
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]

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.

Categories