Why URL parameter is not working rewrite URL htaccess? - regex

I am trying to get blog details page with rewriting URL
like this ->>
www.sitename.com/blog/test-post
My code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([a-z]+) details.php [L]
</IfModule>
My problem is , it is coming the details page, but the blog list page
www.sitename.com/blog/
also redirecting to blog details page. how can i avoid this. i also tried with number code like this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([0-9]+) details.php [L]
</IfModule>
the url with number parameter works fine, but letters is not working. how to solve this?

You need to skip files and directories from your rule:
<IfModule mod_rewrite.c>
RewriteEngine on
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . details.php [L]
</IfModule>

Related

RedirectMatch with or without extension using htaccess

I am trying to do 301 redirects from a few pages to a single URL using .htaccess.
I would like the redirection to work regardless of whether the called page has an extension or not.
But the redirection only works correctly if the page is called with the extension.
If there is no extension, then the page is normally loaded in the browser.
So when I call "mywebsite.com/previous-page-2" the page is displayed well, while I would like it to be automatically redirected to "mywebsite.com/new-page.html"
here is my current code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
RedirectMatch 301 "^(previous-page-1)(.html)?$" "new-page.html"
RedirectMatch 301 "^(previous-page-2)(.html)?$" "new-page.html"
</IfModule>
Please note:
There is also a piece of code added by wordpress to have nice urls (which by the way allows to display the page without extension):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

301 redirect regex for file paths to uploads directory

I am moving a website to wordpress and the site has hundreds of files available for download. The current file path may have a number of subfolders before the name of the file. Below are some examples of how a url may look.
"http://www.example.com/uploads/folder/subfolder/anothersubfolder/file.pdf"
"http://www.example.com/uploads/folder/file.pdf"
"http://www.example.com/uploads/folder/subfolder/anothersubfolder/anothersubfolder/file.doc"
I want to create a 301 redirect to handle any requests for these files and send them to the new url which would be
"http://www.example.com/wp-content/uploads/folder/subfolder"
I'm keeping the sub-folders the same in the uploads folder, so really I need a regex for pointing anything that goes to
"http://www.example.com/uploads/"
and point it to
"http://www.example.com/wp-content/uploads/"
Update
My current .htaccess file looks like
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried adding the answer from anubhava within the <IfModule mod_rewrite.c> block but I think there is an issue with the RewriteBase / from wordpress and me trying to add RewriteBase /uploads/. How can I have a rewrite condition for the /uploads directory?
You can use this redirect rule in DocumentRoot/.htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^uploads(.*)$ /wp-content/uploads/$1 [L,NE,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

.htaccess changes for redirecting requests to root of home page to a static html for wordpress

I have a Wordpress set up which requires redirection when the user enters the root of the site to a static HTML file start.html
http://www.myhomepage.com/
Redirect to
http://www.myhomepage.com/start.html
Wordpress adds url rewrites for calls to index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I only want to redirect
http://www.myhomepage.com/
and not
http://www.myhomepage.com/buy/
This will not work as all requests to Wordpress goes through index.php.
redirect /index.php /start.html
I guess I need a redirect for all pure requests to index.php and not those with query strings. Though I can not figure out how to rewrite it.
The reason is that I want all users that enters the site to get a static html of the wordpress site. Only when the user starts to navigate the site should request be made against wordpress.
EDIT: I required the rule to apply only on GET requests
It can be done without mod_rewrite, with mod_dir and the DirectoryIndex Directive.
DirectoryIndex start.html
Add this line:
RewriteRule ^/?$ /start.html [L]
just after this line:
RewriteBase /
In addition to #Oussama solution, as I have a form which posts to itself on the first page. Posting to start.html would not work. I added a condition so that rule only applies to GET requests. That way form post would be sent to index.php as usual.
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
Final file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^/?$ /start.html [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

How do I set 301 redirects to strip off .html file extensions?

I am working on migrating a MovableType blog to Wordpress.
All of the old post urls end in .html.
Example:
http://ec2-174-129-85-183.compute-1.amazonaws.com/archives/2013/12/all_quiet_on_th.html
However, when I updated the post names in wordpress to include the .html extensions, I kept getting 404 errors.
So what I want to do now is redirect all of the requests for pages that end in .html to the same pages but without the .html.
For example:
.../archives/2013/12/all_quiet_on_th.html
Would 301 redirect to:
.../archives/2013/12/all_quiet_on_th
Here is my current .htaccess file:
# BEGIN WordPress
# Ensure the rewriet module is loaded
<IfModule mod_rewrite.c>
# enable the rewrite engine
RewriteEngine On
# Set the root directory
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Can you please advise what changes I can make to this file for this to work?
I tried 5 or 6 different versions on the internet already, but it is not working and I am not sure why not.
You can try these rules:
# BEGIN WordPress
# Ensure the rewriet module is loaded
<IfModule mod_rewrite.c>
# enable the rewrite engine
RewriteEngine On
# Set the root directory
RewriteBase /
RewriteRule ^(.+?)\.html?$ /$1 [L,NC,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

HTACCESS redirection with a word replacement in url

I'm having trouble with this reg expression which i belive is correct, but it is not working.
What im trying to do is redirect bunch of urls containing a specific string like this:
http://www.example.com/**undesired-string**_another-string to http://www.example.com/**new-string**_another-string
and
http://www.example.com/folder/**undesired-string**/another-string to http://www.example.com/folder/**new-string**/another-string
So i have this code in the .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule (.+)+(undesired-string)+(.+) $1new-string$2 [R=301,L]
</IfModule>
This should replace ANY undesired-string in any url to new-string, but it is not working, any idea why ?
Thank you
Marwen: Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)undesired-string(.*)$ yoursite.com/$1new-string$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ yoursite.com//$1 [L,R=301]
</IfModule>
In your 'updated' code in the comments above, you had it applying the rewrite condition to the undesired-string... So if the actual file or directory was valid it would not rewrite...
Doing this though will always rewrite the undesired-string with new-string - even if its a file name... If that is fine or what you want then all you had to do was move your rewrite conditions to below the rewrite rule...
also.. Just an fyi.. If everything is on yoursite.com you dont need to list yoursite.com
i.e.
yoursite.com/$1new-string$2
just needs to be
/$1new-string$2
which does the same thing: rewrites to the base directory of yoursite.com
now if they are going from mysite.com to yoursite.com then you woulud want to include the domain name because you are redirecting across domain names
Edit: You may also want to use:
[QSA,L,R=301]
instead of just [L,R=301]
Your regex is not really correct. Try:
RewriteRule ^(.*)undesired-string(.*)$ $1new-string$2 [R=301,L]
Or if this doesn't work, try:
RewriteRule ^(.*)undesired-string(.*)$ http://yoursite.com/$1new-string$2 [R=301,L]
Explanation:
^ marks the beginning; $ marks the end; the first (..) goes to $1, the second (..) goes to $2 and so on; * is 0 or more chars; + is 1 or more chars.
To answer my own question. Laravel already redirects the trailing slashes. Problem was that Laravel was installed into a sub-directory. I added the location of the sub-directory to the redirect. My location in this case is: "/lumen/public/". See the fixed htaccess below.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /lumen/public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>