I have next lines in a htaccess file:
RewriteCond %{QUERY_STRING} (^|&)grp=([0-9]+)(&|$)
RewriteCond %{QUERY_STRING} (^|&)level=([0-9]+)(&|$)
RewriteRule ^alpe-adria/([a-zA-Z0-9_-]+)$ /site/index.php?d=1&box=1&grp=%1&grpname=$1&level=%2 [L]
The original URL has this code:
/site/index.php?d=1&box=1&grp=13&grpname=art-culture&level=1
The parameters d and box are fixed, other parameters grp, grpname and level are varying from page to page. What I want to do is to rewrite this URL into such state:
/alpe-adria/art-culture
So the problem is, how to single out grpname parameter and also use all other parameters, from which two are not fixed. I tried two RewriteCond %{QUERY_STRING} rules with no success. Idea is to single out two parameters and then use them in original URL. In other hand one parameter is used in original URL as in substitute URL. Again, I'm stuck and I need your precious help.
Whole snippet:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{QUERY_STRING} box=
RewriteRule ^alpe-adria$ /site/index.php [L]
RewriteCond %{QUERY_STRING} d=
RewriteRule ^alpe-adria$ /site/index.php [L]
RewriteCond %{QUERY_STRING} op=
RewriteRule ^alpe-adria$ /site/index.php [L]
RewriteCond %{QUERY_STRING} masterSearch=
RewriteRule ^alpe-adria$ /site/index.php [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^alpe-adria/?$ /site/index.php?box=1&d=3 [L]
RewriteCond %{QUERY_STRING} (?:|.*&)grpname=([^&]*)
RewriteRule ^site/index\.php$ /alpe-adria/%1? [L,R]
Not very clear from your question but you can use a rule like this which makes your capture all these parameters grp, grpname and level from anywhere in query string:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+site/index\.php\?d=1&box=1&grp=([^&]*)&grpname=([^&]*)&level=([^&]*) [NC]
RewriteRule ^ /alpe-adria/%2/%1/%3? [L,NE,R=302]
RewriteRule ^alpe-adria/([^/]+)/([^/]+)/([^/]+)/?$ /site/index.php?d=1&box=1&grp=$2&grpname=$1&level=$3 [L,QSA]
Do you really have such a non-SEO non-SEF URI coming in from a browser? What is the point of converting it to an SEF form like '/alpe-adria/art-culture'? Unless your new directory structure is actually set up that way, and you're trying to match old non-SEO/SEF URIs to the new layout.
Assuming you actually have incoming '/alpe-adria/*' URIs, you're not rewriting the original URL into /alpe-adria/art-culture. You're using .htaccess to rewrite /alpe-adria/art-culture (coming in from a browser) into the original format that PHP and the server can digest. Please use the correct terminology. .htaccess does nothing to outbound URIs.
You're saying that /alpe-adria/ is fixed, and you have a 'search engine friendly' group name (e.g., 'art-culture'). You want to map 'art-culture' into a URI for index.php? I can't see where you are getting the grp and level entries from in the incoming URI. The best you're going to be able to do is to have a number of lookup entries: match 'art-culture', output the desired full URI. Match something else, output the different desired full URI.
Related
I have a problem with transforming urls from dynamic to static.
I have a site where different pages are generated with a dynamic url, like:
www.example.com/?pr=project-abc123
I would like to rewrite the url of each one with htaccess making it static, like this:
www.example.com/project-abc123
// or
www.example.com/pr/project-abc123
Now, i found this htaccess code that seems to work:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule (.*) http://example.com/%1? [L,R=301]
RewriteRule ^(.*)$ /index/?pr=$1[L]
URLs are rewritten as indicated (first type, whitout /pr/ ), but gives me a multiple choice error. What am I doing wrong?
Let's understand RewriteRule directive which is the real rewriting workhorse.
A RewriteRule consists of three arguments separated by spaces. The arguments are
Pattern: which incoming URLs should be affected by the rule;
Substitution: where should the matching requests be sent;
[flags]: options affecting the rewritten request.
Let's start with the following snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
First line of code, describes RewriteEngine is turned on. Here I use RewriteCond directive which defines a rewrite rule condition. Using RewriteCond directive we defined two conditions here. This directive took a server variable called REQUEST_FILENAME. The two conditions above tell if the request is not a file or a directory, then meet the rule set by RewriteRule. See more details on this issue.
Now it's time to write some rewrite rules. Let's convert
www.example.com/?pr=project-abc123
// to
www.example.com/project-abc123
and rewrite rule will be:
RewriteRule ^(.*)$ index.php/?pr=$1 [L]
And to get the www.example.com/pr/project-abc123 we need the rule as the below:
RewriteRule ^/?([a-z]+)/(.*)$ index.php/?$1=$2 [L]
// or
RewriteRule ^/?pr/(.*)$ index.php/?pr=$1 [L]
The [L] flag tells mod_rewrite to stop processing the rule set. This means that if the rule matches, no further rules will be processed.
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]
I currently use $_GET['base'] to determine which homepage that the user visits.
This results in localhost/?base=administrator or localhost/?base=guest
I am also using this to control which page is the user at, such as
localhost/?base=guest&page=register
Is there any way to use mod_rewrite, or htaccess, to change how this system works?
Modifying my code is not an issue, is this possible?
EDIT:
I am trying to achive this:
localhost/?base=guest to localhost/guest
localhost/?base=admin to localhost/admin
localhost/?base=guest&page=register to localhost/guest/register
Below is my htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
Will the document path affect how it is being called? As I am using a case loop to include which items are needed.
This, however, works for localhost, but it will loop every other address to main.
RewriteEngine On
RewriteRule ^$ /index.php?base=guest[L]
But did not give a result as expected.
Your rules in .htaccess need to be in reverse order, like below:
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
That is because if it is kept in the order you have it, both localhost/?base=guest&page=register & localhost/?base=administrator will match the rule RewriteRule ^([^/]*)$ /?base=$1.
Having them in reverse order ensures that the first rule is matched only for localhost/?base=guest&page=register. It won't match the first rule for localhost/?base=administrator. I hope that helps.
You need to exclude your existent files and folders from the rule
RewriteEngine On
# if the request is a dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
# or file
RewriteCond %{REQUEST_FILENAME} -f
#do nothing
RewriteRule ^ - [L]
RewriteRule ^([^/]*)/([^/]*)$ /?base=$1&page=$2 [L]
RewriteRule ^([^/]*)$ /?base=$1 [L]
So you can use this simple code:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?base=$1 [L]
RewriteRule ^(\w+)/(\w+)$ index.php?base=$1&page=$2 [L]
\w will match symbols a-z, 0-9 and underscore _, I think those characters are enough for your case, but if you need expansion it will be easy
Also in this case you don't need to change your code, because you still get base and page parameters in the $_GET array
UPDATE:
to disable query string params page and base (other params may be needed) add these two lines to the code at the bottom:
RewriteCond %{THE_REQUEST} (\?|&)(page|base) [NC]
RewriteRule .* - [L,R=404]
i am trying to use RewriteRules to get clean urls using my HTACCESS file.
here is what i have so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The above code takes a url that looks like this company.com/about.php and turns it into company.com/about/ so all my links url are like this "/about/" i didnt add the .php because of the rewrite rule.
what i am trying to do now is add a rule that will clean my url when parameter is passed. for example
company.com/about/?profile=member_name i want it to look like company.com/about/member_name
i have tried the two rewrite code below but it doesn't work.
RewriteRule ^([a-zA-Z0-9]+)$ /our_work.php/ [L]
RewriteRule ^/our_work.php([^/\.]+)/?$ ?project=$1 [L]
please keep in mind that my file extension is already being striped from the url.
Please help
Thank you in advance
Apache provides documentation on the use of RewriteRule that you should read before proceeding.
In this particular case, you can rewrite /about/{member_name} to /about.php?profile={member_name} by implementing this rule:
RewriteRule ^about/([a-zA-Z0-9]+)(/)?$ about.php?profile=$1 [L]
The rule states: The requested URL must begin with about/ and be followed by letters and numbers (match #1) and may be proceeded by an additional ending forward slash (match #2); and it will be substitued with a URL about.php with profile query string's value as match #1. The L flag indicates that any rules that follow this rule should be ignored.
Attempting to "cascade" rules will not work with your current set of rules because the /about/{...} is not matched by the first rule.
I have written this little piece of code. I am very new to this so i am not sure it is all correct. but basically it lets me access urls with the php extension. When people get on the site they are being redirected from the geo ip page to the correct language which like looks like this
main.php?lang=uk or nl or en or eu etc.
right now i can also use it like this
main/?lang=uk or nl or en or eu etc.
I would like to be able to to also remove the variable in the url ?lang=uk.
How would i do this. My .htaccess code is below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
DirectorySlash On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R=301,L]
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
</IfModule>
Thanks too anyone willing to help.
The first argument of RewriteRule does match anything after the domain name and prefix* and before any query string if that exists. In http://localhost/this/is/my/test?lang=123 with a .htaccess file in the this/is/ directory it would match my/test. To match a query string, you have to use the %{QUERY_STRING} variable.
If the second argument (the rewritten url) of RewriteRule does not contain a query string, it will automatically append the query string of the original url to the new url.
In the code below I use %{THE_REQUEST}. This is the string that is used to make the request for a page. It is in the form of GET /my/resource?query=string HTTP/1.1. It does not change when rewriting things, which makes it useful to prevent infinite loops.
In your php file make sure that the language is read from a cookie instead of a get variable, then do the following:
#Set cookie for language
RewriteCond %{QUERY_STRING} ^(.*?)&?lang=([^&]+)&?(.*?)$
RewriteRule ^ %{REQUEST_URI}?%1&%3 [CO=lang:%2:127.0.0.1:1:/:0:1,R,L]
#Remove potential prefix or suffix & from query string
RewriteCond %{QUERY_STRING} ^&(.*?)$ [OR]
RewriteCond %{QUERY_STRING} ^(.*?)&$
RewriteRule ^ %{REQUEST_URI}?%1 [R,L]
#External requests with \.php should be without that.
RewriteCond %{THE_REQUEST} \.php
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#Try to load php page if resource does not alreay have an extension
RewriteCond %{REQUEST_URI} !\.[a-z]+$
RewriteRule ^(.*)$ $1.php [L]