URL rewrite for multilanguage site with .htaccess (Apache) - php

I can't figure out how to cut extension (.php) in .htaccess in case I already have 1 virtual sub directory.
1) localhost/admin/index.php?lang=en -> localhost/admin/en/
2) localhost/admin/index.php?lang=ru -> localhost/admin/ru/
For case 1) and 2) I wrote already and it works.
Resolved with -> RewriteRule ^([a-z]+)(/)?$ index.php?lang=$1 [QSA,L]
Please consider that (en & ru are virtual directories)
3) localhost/admin/index.php?lang=en/createprofiles.php -> localhost/admin/en/createprofiles.php
4) localhost/admin/index.php?lang=ru/createprofiles.php -> localhost/admin/ru/createprofiles.php
For case 3) and 4) I wrote also and it works.
RewriteRule ^([en|ru]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
But I would like to cut .php extension.
The final link must be like : localhost/admin/en/createprofiles/
Can you please help me :). I have tried everything (about 8 hours), but nothing works
Thank you !

This would be an example which implements both directions:
requests to the "pretty URLs" are internally rewritten to the actual scripts and
(possible) requests to the actual scripts are externally redirected to the "pretty URLs"
This obviously is just an example, your real world scenario is typically a bit more complex. But this example should point you into the right direction...
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/?$ /admin/%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/index\.php$ /admin/%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(en|ru)(?:&|$)
RewriteRule ^/?admin/createprofiles\.php$ /admin/%1/createprofiles [QSA,R=301]
RewriteRule ^/?admin/(en|ru)/?$ /admin/index.php?lang=$1 [QSA,END]
RewriteRule ^/?admin/(en|ru)/createprofiles/?$ /admin/createprofiles.php?lang=$1 [QSA,END]
It is a good idea to start out with a 302 temporary redirection (instead of the R=301) and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

How to redirect query string to subdirectory based url using .htaccess?

Hi i tried lot of examples but nothing is working for me.
I need to redirect
From : example.com/category.php?category_id=1&category=பொது
To : example.com/category/1/பொது/
I am using following htaccess
DirectoryIndex index.php
AddDefaultCharset utf-8
RewriteEngine On
RewriteCond %{QUERY_STRING} category_id=$1&category=$2
RewriteRule ^category\.php$ /$1/$2/? [L,R=301]
It's redirect to something like this
example.com/category/%25E0%25AE%25AA%25E0%25AF%258A%25E0%25AE%25A4%25E0%25AF%2581
also showing page not found. Someone please help me. Thanks.
This probably is what you are looking for:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)category_id=([^&]+)&category=([^&]+)(?:&|$)
RewriteRule ^/?category\.php$ /category/$1/$2 [QSD,END,R=301]
RewriteRule ^/?category/([^/]+)/([^/]+)/? /category.php?category_id=$1&category=$2 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Rewrite the url of a website living in a subdirectory with .htaccess

I'm currently programming a website that, in a nutshell, loads its content dynamically via url parameters and php includes. The website is living under the root directory in a subfolder called "saischek".
The urls have one optional parameter: page, therefore the urls can for example look like this:
localhost/saischek/index.php?page=accounting
localhost/saischek/index.php
I would like to have that my urls look like this:
localhost/saischek/accounting
localhost/saischek/home <- if the url parameter _page_ isn't given
My .htaccess file is currently living in the subdirectory "saischek" and looks as follows:
RewriteEngine on
RewriteRule ^saischek/([a-zA-Z0-9_-]+)$ saischek/index.php?page=$1
The website is running on apache webserver and all necessary changes in "httpd.conf" file are done and working.
I'd say with the discussion in the comments to the question and your revision of the question you are nearly there, you found the solution yourself...
I would recommend to slightly change the main rule, also you will need to handle the "home" page you mentioned
RewriteEngine on
RewriteCond %{REQUEST_URI} !/saischek/index\.html$
RewriteRule ^/?saischek/home/?$ /saischek/index.php [QSA,END]
RewriteRule ^/?saischek/([a-zA-Z0-9_-]+)/?$ /saischek/index.php?page=$1 [QSA,END]
I wonder however why you want to use the "home" slug at all, why not simply use https://example.org/saischek instead of https://example.org/saischek/home? In that case you would try this variant:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/saischek/index\.html$
RewriteRule ^/?saischek/?$ /saischek/index.php [QSA,END]
RewriteRule ^/?saischek/([a-zA-Z0-9_-]+)/?$ /saischek/index.php?page=$1 [QSA,END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Try adding ? and changing + to * because in case the page is missing, the URL would not have them both.. (both / and page).
^saischek/([a-zA-Z0-9_-]+)/?([a-zA-Z0-9_-]*)$

How redirect to subfolders using .htaccess file?

I'm new in php and trying to build my website.
I have created folder structure on my server listed below.
development (folder)
admin (folder)
.htaccess ( copied from internet)
index.html (file with coming soon banner)
I want to set my .htaccess file to load index.html file by default when users open (abcd.com).
And subfolders would access with hitting the url like abcd.com/admin or abcd.com/developement.
But I did some stupid changes in .htaccess file so it redirect the users to my admin login page which i don't want them to see. ;(
Thanks in Advance.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?abcd.com$
RewriteCond %{REQUEST_URI} !^/admin/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /admin/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?abcd.com$
RewriteRule ^(/)?$ /admin/web/index.php [L]
It is a bit unclear why you actually ask this question...
As a web developer you surely should be able to find your way around the rewriting options the apache http server offers by looking into the documentation. But even if not: as #AmanjotKaur pointed out the default behavior of that http server is to serve the index.html file if present. If that is not the case then you should take a look at the DirectoryIndex directive offered by mod_dir... Also it should be easy to simply roll back to an older version of that dynamic configuration file you used (".htaccess"), if you are using a revision control system for your development as you certainly should. Or by using a simple backup which you hopefully have ...
A general hint here is to simplify the situation: instead of trying to implement rules to direct incoming requests simply separate the contents. Your "production site" (which I understand is not usable yet) should not host any content, but just that placeholder you already have. Development should not be done on the same site! Use a separate http server for that or at least a separate host name inside the same http server. That is much easier and offers much more reliable protection.
Anyway, there are situations where one indeed wants to redirect all incoming requests to some specific document or router script, with a few explicit exceptions. This might be what you are looking for here. Since your question is a bit vague in that (your question mentions the path /admin, but your rules implement the path /admin/web/...) we can only give you a starting point. You will have to make your way from there. For which you undoubtedly need to take a look into the documentation of the apache http server's mod_rewrite which you want to use on that dynamic configuration file.
Here is something whici I assume implements something along the lines of what you are looking for. You certainly need to adapt it to your needs, though. You'd need to revise your question and be much more precise in what you actually want if you need further help with that. There is an edit button below your question. Use it ...
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/development/
RewriteRule ^ /index.html [R=301]
RewriteCond %{REQUEST_URI} !^/admin/web/
RewriteRule ^/?admin/(.*)$ /admin/web/$1 [END]
RewriteCond %{REQUEST_URI} !^/development/web/
RewriteRule ^/?development/(.*)$ /development/web/$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How do I redirect any subdomain through htaccess as GET parameter?

So if the user enters london.abcd.com, it needs to go to abcs.com/show.php?loc=london while still showing the URL london.abcd.com
Another example:
(Enters) sydney.abcd.com -> (goes to) abcd.com/show.php?loc=sydney and the URL shows sydney.abcd.com
How do I achieve this via HTACCESS both for localhost(XAMPP on Windows) and the actual server online?
Tried this so far but doesn't work. Redirects me back to index.php
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.com [NC]
RewriteRule (.*) https://www.%2.com/show.php?loc=%1 [NC,L]
This would be a working variant of what you suggest. Note however that it will ignore any path specified within the requested URL:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.exmaple\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.com$
RewriteRule ^ https://www.example.com/show.php?loc=%1 [R=301,QSA]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In your comment to this answer you point out that you actually do not want a redirect, as written in the question, but an internal rewrite. So the visible URL in the browser should not be altered, only the internal processing should be. This is possible with a slight modification:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.exmaple\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.example\.com$
RewriteRule ^ /show.php?loc=%1 [END,QSA]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

htaccess with 2 parameters

I want to make a url like this
if the parameter is only one
it will be localhost/site/news from index.php?page=news
this is the .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
but if the link like this index.php?page=news&halaman=2
i want to make it like this localhost/site/news/2
i have try this but its not working
RewriteRule ^(.+?)/(.+?) index.php?p=$1&halaman=$2 [NC,L]
how can i resolve this? Thanks...
Your attempt points into the right direction, I only took the liberty to clean it up a bit:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^/?(\w+)/?$ /index.php?page=$1 [END,QSD]
RewriteCond %{REQUEST_URI} !/index\.php$
RewriteRule ^/?(\w+)/(\d+)/?$ /index.php?page=$1&halaman=$2 [END,QSD]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Categories