Convert links using Apache without redirection - apache

I need to convert links from /s/categories/... to /s/blog/categories/... without redirection
For instance, from:
https://example.com/s/real-estate/whatever-else-links/
to:
https://example.com/s/blog/real-estate/whatever-else-links/
Similarly there are few other categories like real-estate that need to be converted from /s/categories/ to /s/blog/categories/
To make things simple I can enter the other categories logic separately since there are few.
Upon some research I tried the following in the .htaccess file that does not work:
RewriteCond %{HTTP_HOST} !^/s/real-estate/([^\.]+)$
RewriteRule ^/s/barcelona-real-estate/([^\.]+)$ /blog/real-estate/$1
Here is the rest of the .htaccess file is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /s/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /s/index.php [L]
RewriteCond %{HTTP_HOST} !^/s/barcelona-real-estate/([^\.]+)$
RewriteRule ^/s/barcelona-real-estate/([^\.]+)$ /blog/barcelona-real-estate/$1
</IfModule>
Please let me know how to convert the links at the web server level. Thanks very much.

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

htaccess remove folder redirect

I have a problem removing folders from an url. I want that google / old links aren't broken. The old webpage had several sections with a structure like this
example.com/news/items/entry1.html
example.com/news/items/entry2.html
example.com/blog/items/foo.html
The new page has the urls like this:
example.com/news/entry1
example.com/news/entry2
example.com/blog/foo
Removing html was rather straight forward
<IfModule mod_rewrite.c>
RewriteEngine On
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php [QSA,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
</IfModule>
The part I'm struggling with is removing the 'items' part. The rules I found only worked for request path like 'example.com/items/subfolder1/...'
Any help would be greatly appreciated.
To remove /items/ from your URLs you can use the following in your .htaccess file:
RewriteEngine On
RewriteRule ^(.*)/items /$1 [L,R=301]
So for example, this will take the URL: http://example.com/news/items/entry1 and turn it into http://example.com/news/entry1
Make sure you clear your cache before testing this.

how to rewrite URL using .htaccess file?

I am having problem in rewriting a url using .htaccess file. I am not aware of regex. I have red so many blogs and used online rewrite rule generator to generate url and pasted in .htaccess file. But no one is worked. I have enabled mod_rewrite module in httpd.conf file in apache server.
I want to rewrite a below url.
I have developed project in OS - Win7 with Apache server.
http://localhost:81/quora/tamilnanban/innerpage.php?url=why-you-should-avoid-watching-porn-on-android-smartphones
to
http://localhost:81/quora/tamilnanban/why-you-should-avoid-watching-porn-on-android-smartphones
my current .htaccess file contains below codes:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
#RewriteRule ^url/([^/]*)\.html$ :81/quora/tamilnanban/innerpage.php?url=$1 [L]
RewriteRule ^/quora/tamilnanban/([^/]*)\.html$ /quora/tamilnanban/innerpage.php?url=$1 [L]
This above rule also not working. Kindly help me to achieve url rewriting using .htaccess file. Also if i am doing any mistakes in the above steps, please correct me. Thanks in advance.
To convert /quora/tamilnanban/innerpage\.php\?url=foobar" to /quora/tamilnanban/foobar you can use the following rules in /quora/tamilnanban/.htaccess :
RewriteEngine on
#1)Redirect "/quora/tamilnanban/innerpage\.php\?url=foobar" to "/quora/tamilnanban/foobar"#
RewriteCond %{THE_REQUEST} /quora/tamilnanban/innerpage\.php\?url=(.+)\sHTTP [NC]
RewriteRule ^ /quora/tamilnanban/%1? [L,R]
#2)The rule bellow will internally map "/quora/tamilnanban/foobar"" to "/quora/tamilnanban/innerpage\.php?url=foobar"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /quora/tamilnanban/innerpage\.php?url=$1 [L]

htaccess and CakePHP3 routing (multiple "sites" same server)

Okay so I need just a little bit of help, I think i almost have my routing clean but am running into one small issue.
I am trying to setup one server to handle multiple different static sites as well as a cakephp 3 installation.
The root .htaccess has the following
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{ENV:REDIRECT_SUBDOMAIN} =""
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9][-a-z0-9]+)\.domain\.com(:80)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{DOCUMENT_ROOT}/%2 -d
RewriteRule ^(.*) /%2/$1 [E=SUBDOMAIN:%2,L]
RewriteRule ^ - [E=SUBDOMAIN:%{ENV:REDIRECT_SUBDOMAIN},L]
This works great for routing subdomains to their proper folder
static.domain.com properly goes to /static/index.php
the problem comes when i try to access my cake installation
cake.domain.com ends up routing to cake.domain.com/cake/$1
I really need it to route to cake.domain.com/$1
/cake/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cake
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/cake/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /cake
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(webroot/)?(img|css|js)/(.*)$
RewriteRule ^ index.php [L]
</IfModule>
I will be eternally grateful for any help, i have exhausted google searching and banging my head on the wall.
I am completely open to easier ideas as well

URL Rewrite Rule using .htaccess not working as expected

In continuation with my previous Question url-rewrite-rule-based-on-certain-conditions
I am trying to do a URL rewrite like this
http://www.mydomain.com/wp-content/plugins/pluginA/abc.css
http://www.mydomain.com/wp-content/plugins/pluginA/abc.js
i want that when the CSS and JS are from this plugin (Plugin A ) or any other plugin whose URL i want to rewrite, The URL should get rewritten as
http://www.cdn.mydomain.com/wp-content/plugins/pluginA/abc.css
http://www.cdn.mydomain.com/wp-content/plugins/pluginA/abc.js
This is what i tried as in my .htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} ^.+\.(css|js)$
RewriteRule ^wp-content/plugins/(nivo-slider-light|Usernoise|lightbox-plus|wp-content-slideshow|content-slide)/(.+)$ http:/abc.cloudfront.net/wp-content/plugins/$1/$2 [L,R=302,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
I have tried every possible way to rewrite the URL so that JS and CSS should get served from my CDN server, but seems like the rule is never been picked up by apache.Any help/pointer in this regard will really be appreciated.
Make this your first rule (right after RewriteBase line)
RewriteRule ^(wp-content/plugins/(nivo-slider-light|Usernoise|lightbox-plus|wp-content-slideshow|content-slide)/[^.]+\.(css|js))$ http://abc.cloudfront.net/$1 [L,R,NC]
Also if it doesn't work try moving your *.js and *.css files away from local directories under wp-content/plugins/ path.