There is the daily question again, how can I rewrite my .htaccess. I found there is a lack of information about the .htaccess rules and can not find an answer to the following:
RewriteEngine On
RewriteRule ^user/([^/]*)$ /user.php?username=$1 [L]
Now this rule makes sure that users.php?username=hello rewrites as user.php/hello
but how can I get rid of the .php? Do i add the following condition and change the previous condition without the .php? so
RewriteEngine On
RewriteRule ^user/([^/]*)$ /user?username=$1 [L]
Or do I keep the previous one WITH php? I found the following condition to remove the php.
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
however it is NOT working in combination with the user.php.
How can I rewrite user.php?username=hello to user/hello and in the same time have normal .php files as index.php to index.
Related
I am trying to learn how to use RewriteRule.
I have 2 'pages' (wordpress)-
domain.com/webinars/ (shows list of all webinars)
and
domain.com/webinar/ (shows specific webinar details)
I have trying to set the following conditions -
(1) domain.com/webinars/{Year}/{Month}/ will load /webinar/?year={Year}&month={Month}
(2) domain.com/webinar/ (with no /{Year}/{Month}) will load /webinars/
(3) domain.com/webinar/?year={Year}&month={Month} will redirect to /webinars/{Year}/{Month}/ and then apply condition #1
This is my attempted code
RewriteEngine On
RewriteRule ^/webinars/([0-9]+)/([A-Za-z0-9]+)/$ /webinar/?year=$1&month=$2 [NC]
RewriteRule ^/webinar/$ /webinars/
RewriteRule ^/webinar/year=(\d+)$month=([\w-]+)$ /webinars/%1/%2? [R=301,L]
Condition #1 results in a 404 page not found
Condition #2 shows /webinar/ and not /webinars/
Condition #3 stays on domain.com/webinar/?year={Year}&month={Month} and does not redirect
What am I doing wrong? Only other code in my htaccess file is the default wordpress block.
Try the following :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^webinars/([^/]+)/([^/]+) /webinar/?year=$1&month=$2 [NC]
The rest could be done the same way .
Try with below rules,
RewriteCond %{REQUEST_URI} ^/webinar$
RewriteCond %{QUERY_STRING} ^year=([^/]+)&month=([^/]+)
RewriteRule ^ http://example.com/webinars/%1/%2/? [R=302]
RewriteCond %{REQUEST_URI} ^/webinars/([^/]+)/([^/]+)/$
RewriteRule ^ webinar/?year=%1&month=%2 [L,END]
RewriteCond %{REQUEST_URI} ^/webinar/$
RewriteRule ^ webinars/ [L]
You can use this
RewriteEngine on
# if /webinar/ is requested skip the rules and serve /webinar/ directory
RewriteRule ^webinar/?$ - [L]
#redirect /webinar/?year={year}&month={month}
#To /webinar/year/month
RewriteCond %{THE_REQUEST} /webinar/\?year=([^\s&]+)&month=([^\s&]+) [NC]
RewriteRule ^.+$ /webinar/%1/%2? [L,R]
# rewrite new url to the old one
RewriteRule ^webinar/([^/]+)/([^/]+)/?$ /webinar/?year=$1&month=$2 [L,NC]
I am rebuilding a real estate site and we have completely restructured how the database handles the properties and I am now trying to redirect all of the old property urls to the new ones. The problem that I am having is that the old urls used multiple parameters(including the section and the property id) for a single property and the new urls only use a slug for the property name.
Example:
OLD: https://www.example.com/listings.php?sect=1&view=92
NEW: https://www.example.com/listings/tombstone-ranch
My current .htaccess looks like the following with the last two lines being the rewrites for converting the slugs into clean urls...all of this works fine.
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteRule ^listings/([a-zA-Z0-9-/]+)$ /listings/index.php?s=$1 [L]
RewriteRule ^listings/([a-zA-Z0-9-/]+)/$ /listings/index.php?s=$1 [L]
The Problem that I am having is that neither of the following seems to work:
Redirect 301 /listings.php?sect=1&view=92 /listings/tombstone-ranch
and neither does this:
RewriteRule ^listings.php?sect=3&view=33 /listings/tombstone-ranch
or any other variations that I have tried.
Any thoughts? .htaccess is not my strong suit and unfortuinately I need to get these to work considering the old version of the site and it's urls have been around for almost 8 years now so there is the potential for dead links on about 75 properties.
Could you try this rule. Can't test it at the moment.
RewriteCond %{QUERY_STRING} sect=1&view=92
RewriteRule ^listings\.php$ /listings/thombstone-ranch/? [L,R=301]
Also the rule you added will not work because you must add a backslash
Wrong:
RewriteRule ^listings.php?sect=3&view=33 /listings/tombstone-ranch
Right:
RewriteRule ^listings\.php?sect=3&view=33 /listings/tombstone-ranch
Don't forget since your main URL is a query, you should use the first option that I gave to you. If that doesn't work leave a comment and I'll be glad to help you out
I am having difficulty configuring a mod_rewrite rule in my .htaccess file to hide certain subdirectories.
Input:
https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
Desired output:
https://example.com/en-US/additional_sub/file
I had code that allowed me to hide something if I specifically name it:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /hidden_sub1/hidden_sub2/ [NC]
RewriteRule ^hidden_sub1/hidden_sub2/(.*) /$1 [L,R=301]
RewriteRule !^hidden_sub1/hidden_sub2/ hidden_sub1/hidden_sub2%{REQUEST_URI} [L]
However, it stopped working once I added in the following language subdirectory code:
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]
Besides, the name of the subdirectory is dynamic. "hidden_sub1" might be "random_name243". However, they would always be the first two subdirectories.
So how do I take out the first two randomly-named subdirectories while still keeping the language subdirectory?
I appreciate any help.
To change your url from the form
https://example.com/hidden_sub1/hidden_sub2/additional_sub/file.php?lang=en-US
To the form :
https://example.com/en-US/additional_sub/file
You can use the following Rewrite rule
RewriteEngine On
RewriteBase /
#1)externally redirect "/hidden_sub1/hidden_sub2/foo.php?lang=bar" to "/bar/foo"
RewriteCond %{THE_REQUEST} /hidden_sub1/hidden_sub2/([^.]+)\.php\?lang=(.+)\sHTTP [NC]
RewriteRule ^ /%2/%1? [L,R=301]
#2)internally rewrite "/bar/foo" to "/hidden_sub1/hidden_sub2/foo.php?lang=bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /hidden_sub1/hidden_sub2/$2.php?lang=$1 [NC,L]
Tested on apache 2.2. and 2.4.
I am a new bee and I want to redirect following URL
m.site.com/subfolder/index.php?user=xxx
to a search engine frendly url like below using .htacess mode rewrite
m.site.com/subfolder/xxx
please explain me the correct way
You can use in the .htaccess file, in the subfolder (It is important):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{QUERY_STRING} ^user=(.+)$
RewriteRule ^ %1? [R=302,L]
RewriteRule ^(.*)$ index.php?user=$1 [L]
Change R=302 for R=301 when test work well.
I have:
mydomain.com/folder-name/segment1/segment2
I want to change it to:
mydomain.com/segment1/segment2
using a 301 redirect.
I've tried:
RewriteCond %{REQUEST_URI} !^/test/.*$
RewriteRule ^(.*)$ /test/$1 [L]
but its not working
here is my htacess file:
# #AddHandler application/x-httpd-php53 .php .php5 .php4 .php3
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/b1/.*$
RewriteRule ^(.*)$ /b1/$1 [R=301,L]
The answer for the first part of the question should be like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/? $2/$3 [R=301,L]
The second code that you've tried is the opposite of what you're asking for initially. This line matches anything not starting with /test/:
RewriteCond %{REQUEST_URI} !^/test/.*$
This line says take everything and rewrite it to the /test/ directory:
RewriteRule ^(.*)$ /test/$1 [L]
So together anything that's not in the test directory is being rewritten to the test directory.
If you're trying to specifically remove the word test then you would remove the ! symbol in your attempt to create a match. Since you already know it's called test there's no need to even make Apache perform this look for 'test' because Apache handles the RewriteCond statement after the RewriteRule (rather unintuitively).
RewriteCond %{REQUEST_URI} ^/?test
You can specialize the rewrite rule like this (I've added [QSA] to catch any query strings:
RewriteRule ^test/([^/]+)/([^/]+)/? $1/$2/ [R=301,L,QSA]
Simply change your code to:
RewriteRule ^test/(.*)$ /$1 [R=301,L,NC]