I have this below URL and all # symbols needs to replaced with _
http://localhost/test/my#module/my#index.php?param2=10¶m2=10
i tried below .htaccess code, but not working. I searched a lot for solution , but none of them are working.
Options +FollowSymlinks -MultiViews
RewriteEngine on
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([#]*)[#]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([#]+)$ $1 [R=301,L]
Please let me how can fix this issue.
Related
I have a hard time to create rewrite rule for a redirect using part of an old URL for WP. Example:
Old URL:
http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site
or
http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site
New URL:
http://www.example.com/2014/11/07/my-blog-post
New URL should to have only dates and first three elements of a permalink after stripping from dashes.
My solution came after combining answers from here https://stackoverflow.com/a/32852444/1090360 and here https://stackoverflow.com/a/1279758/1090360
Somehow part for replacing underscores with dashes creates infinite redirect and a server freezes. If I will remove part with replacing underscores to dashes all the rest works as should.
Here are my .httaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#replace underscores with dashes
RewriteRule ^(/news/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/news/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301,L,NC]
#redirect to new URL
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]
#WP standard stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I think, this is an expensive way to replace underscores with dashes. But this works at least in my test environment. The first rule replaces dashes one by one. The second rule then removes the prefix from the requested URL.
RewriteBase /
# replace underscores with dashes
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [L]
# strip "news/index.php"
RewriteRule ^news/index.php/(.*) /$1 [R=302,L]
I played a bit more with your original approach using the N|next flag and crashed my server too. Looking into the error.log, it seems this infinite loop is created by Apache by adding a "path info postfix", which enlarges the URL with the original URL. And so it keeps replacing underscores with dashes on and on.
You can prevent this path info postfix with another flag DPI|discardpath, which gives the following rule
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [N,DPI]
This seems to work too. Although I must admit, I don't really understand this "path info postfix" thing. There's also an entry in Apache's Bugzilla, Bug 38642: mod_rewrite adds path info postfix after a substitution occured
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I know many user have asked this question but i have tried everything still not understand the problem
I have url like this
http://localhost/test/storelocator.php?page=store
Now i want to like this
http://localhost/spawake/storelocator
My htaccess
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for storelocator.php
RewriteRule ^storelocator storelocator.php [NC, L]
but showing me the error in all the pages Internal Server Error So anyone can plz hep
Remove space after NC
Disable MultiViews option
Restrict your pattern by using end anchor.
Your .htaccess:
Option -MultiViews
RewriteEngine on
RewriteBase /spawake/fbdemo/
# Rewrite for storelocator.php
RewriteRule ^storelocator/?$ storelocator.php [NC,L]
I am facing an issue in url re-writing in .htaccess. My url data coming from Database which include some spaces as well. I want to omit spaces from my url's and want to replace it with dashes.
currently what i am getting with my current .htacess ..
http://www.xyz.com/detail-10-Event%20Tickets.html
I want it to be replaced with
http://www.xyz.com/detail-43-61-Event-Tickets.html (This is what i want.)
Please find the code for .htaccess and suggest what changes should i made to solve this issue.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule uploadPRODUCT-(.*)-(.*)-(.*).html$ uploadPRODUCT.php?cid=$1&aid=$2&tid=$3
RewriteRule ab-(.*)-(.*).html$ products.php?cid=$1&cname=$2
RewriteRule detail-(.*)-(.*)-(.*).html$ productDETAILS.php?cid=$1&aid=$2&pname=$3
RewriteRule (.*)-(.*).html$ cms.php?name=$1&cmsid=$2
errorDocument 404 http://www.xyz.com/notfound.php
errorDocument 500 http://www.xyz.com/500error.html
RewriteCond %{http_host} ^xyz.com.com [NC]
RewriteRule ^(.*)$ http://www.xyz.com/$1 [R=301,L]
</IfModule>
Since you create the URLs from database entries, I would replace the spaces at URL creation time. You can use str_replace for this
$url = str_replace(' ', '-', $db_column);
I want to use .htaccess mod_rewrite to remove .php from all my files and force a trailing slash /. However, it is only resulting in me getting server errors.
You want to map :
http://sampledomain.ext/index/ -> http://sampledomain.ext/index.html
Use the following .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} ! \.php$ #Avoid real php page, do what you want with them
RewriteCond %{REQUEST_URI} /$ #Ensure a final slash
RewriteRule ^(.*)$ $1.php #Add your extension
Tx to David Wolever who write quitely the same stuff 1
If I can give my opinion, it's a little bit strange to force the / at the end for a file no?
Do I need a slash before or after the /somefolder/(.*) below? I want to redirect everything coming into a folder to another website address. Do I need the RewriteCond in here somewhere also?
Options +FollowSymLinks
RewriteEngine on
RewriteRule somefolder(.*) http://www.differentsite.com$1 [R=301,L]
You don't need a / at the beginning, but a caret ^ will match http://whatever.com/ including the trailing /.