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).
Related
I need to use .htaccess to load index file from specific directory when user vitis an url that does not exists.
Here is my folder structure:
root/realdirectory/virtualdirectory/virtualproducturl
my index file is located in root/realdirectory
And something else which makes the task more complecated :
root/realdirectory/index.php has to be loaded if user visits:
-root/realdirectory/virtualdirectory/virtualproducturl or
-root/realdirectory/virtualdirectory/
The 'virtualdirectory' is my category of the product and when the user visits just the category url I want to show him some information from root/realdirectory/index.php
Thank you in advance
Sounds pretty straight forward: when a request does not get resolved to an existing file or folder, then internally rewrite it to an "index document":
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^ /index.php [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).
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).
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_-]*)$
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
I am trying to redirect all query strings off the root to a specific page on my site. The problem I'm having is making sure that query strings on other pages and directories still work as normal (for example domain.com/page/query=2 should not redirect).
This is what I've got:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /page-not-found/ [R=301,L,QSD]
Any help is appreciated - thanks!
An unexpected requirement, but OK, this probably is what you are looking for:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^/?$ /page-not-found/ [R=301,QSD]
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).