Apache mod_rewrite: URL slug not updating - php

There are a few SO questions about mod_rewrite such as here and here, but so far I cannot find one that works with my example.
I have an Apache webserver running my PHP website at the root var/www/html. at /var/www/html/news I have a .htaccess file which will be used to convert ugly $_GET filled url string into SEO friendly urls.
My current url looks like this:
https://mywebsite.com/news/article.php?id=2&title=myfirstarticle
I would like my url to look like this:
https://mywebsite.com/news/2/myfirstarticle
Here is my .htaccess file, currently redirecting to the indended url structure.
RewriteEngine on
RewriteRule ^([^/]*)/([^/]*)$ /article.php?id=$1&title=$2
So far my URL is not being changed to the SEO friendly URL, what am I missing?

You need to handle both directions: the external redirection and the internal rewriting:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)id=(\d+)&title=([^&]+)(?:&|$)
RewriteRule ^article.php$ /news/%1/%2 [R=301]
RewriteRule ^([^/]+)/([^/]+)$ /article.php?id=$1&title=$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 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).

Try the below htaccess rule:
RewriteEngine On
RewriteRule ^news/([^/]*)/([^/]*)$ /news/article.php?id=$1&title=$2

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_-]*)$

URL rewrite for multilanguage site with .htaccess (Apache)

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).

Rewrite root directory and index.php to custom URL in browser

I'm using XAMPP 7.2.6 on Windows and my root web folder is htdocs. I have the mod_rewrite module installed in Apache.
I have a website project in C:/xampp/htdocs/xyz
I want to use mod_rewrite to rewrite the URL of the root directory and index.php to a custom URL in the browser - index.php?action=viewFrontPage&pageId=2
.htaccess file
RewriteEngine on
RewriteRule ^/index.php? /index.php?action=viewFrontPage&pageId=2
This does nothing and I can't figure it out.
There are a few issues with your approach, but I will point out the first and most obvious:
Your current configuration
RewriteEngine on
RewriteRule ^/index.php? /index.php?action=viewFrontPage&pageId=2
does not express what you expect it to express and in fact that rule will never get applied. Why? Check the documentation! That is where you can read that the pattern will get matched against a relative path of a rewriting rule is implemented in a dynamic configuration file (".htaccess"). You try to match an absolute path.
That is why I would suggest this as a first step towards your goal (which is actually unclear from your question):
RewriteEngine on
RewriteRule ^/?index\.php$ /index.php?action=viewFrontPage&pageId=2 [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 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).
That adapted rule will not work. This brings us to step 2... The issue you now face is that you have created an endless rewriting loop. You redirect /index.php to /index.php... not a good idea for obvious reasons. So you need to prevent such a loop, but for that you will need to be able to tell how the server should distinguish between the two situation. Are you able to?
This might be a start, but you will certainly need to adapt it:
RewriteEngine on
RewriteCond %{QUERY_STRING} !^action=viewFrontPage&pageId=2$
RewriteRule ^/?index\.php$ /index.php?action=viewFrontPage&pageId=2 [END]
Also also you wrote that you want to rewrite the "root directory", but it is unclear what you actually mean by that...

Change Wordpress page URL with parameter

I have created a dynamic content page on my Wordpress site. The content is taken from the MySQL database and will be shown based on the GET URL parameter. I created this dynamic page by inserting a PHP code to a Wordpress Page (I use Advanced Ads plugin to insert the code)
I want to change the URL without an URL parameter.
Example:
DomainName.com/hotel-details/?hotelcode=First-Hotel
I want to change it to
DomainName.com/hotel-details/First-Hotel
or
DomainName.com/sometext/First-Hotel
I have tried to adding mod_rewrite to the .htaccess file in the root folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^hotel-details/([A-Za-z0-9-]+)$ hotel-details/?hotelcode=$1 [NC,L]
</IfModule>
But it seems doesn't work, when I access DomainName.com/hotel-details/First-Hotel I got an error 404 Page Not Found.
Any help much appreciated!
You are using the rule the wrong way 'round. Instead try that:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?hotel-details/(\w+)/$ /hotel-details/?hotelcode=$1 [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 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