how to shorten url using htaccess - .htaccess

Hello guys i need to shorten my url using htaccess
like
Before
www.mysite.com/obj/task/profile/id/1568/username
after
www.mysite.com/1568/username
or
Before
www.mysite.com/obj/task/page/city/41280/pageId/22/clubs//Test%20town
after
www.mysite.com/41280/22/clubs//Test%20town
how can i achieved this using htaccess
my htaccess file has this
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]
Suggested Changed
<files .htaccess>
order allow,deny
deny from all
</files>
php_value memory_limit 170M
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
#RewriteCond %{HTTPS} !=on
#RewriteCond %{HTTP_HOST} !^$ [NC]
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# THIS RULE FOR FOR INACTIVE HTTPS
#RewriteCond %{HTTP_HOST} !^www.*$ [NC]
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# from /obj/task/profile/id/1568/username
# to /1568/username
RewriteRule ^obj/task/profile/id(/.+)$/$1 [R=302,L,NC]
# from obj/task/page/city/41280/pageId/22/clubs//Test%20town
# to 41280/22/clubs//Test%20town
RewriteRule ^obj/task/page/city/([^/]+)/pageId(/.+?)/?$ /$1/$2 [R=302,L,NC,NE]
I have visited www.mysite.com/obj/task/profile/id/1568/username
The links is still not shorten.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# from /obj/task/profile/id/1568/username
# to /1568/username
RewriteRule ^obj/task/profile/id(/.+)$ /$1 [R=302,L,NC]
# from obj/task/page/city/41280/pageId/22/clubs//Test%20town
# to 41280/22/clubs//Test%20town
RewriteRule ^obj/task/page/city/([^/]+)/pageId(/.+?)/?$ /$1/$2 [R=302,L,NC,NE]

Related

.htaccess redirect loop with RewriteCond

I am trying to redirect a specific page
http://www.my.website/admin
to
https://my.website/admin/
using this code:
Redirect 301 /admin/ https://my.website/admin/
but it keeps looping and I can not find out why as I have specified the condition for 'www' not to be present.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.tci\.rocks$ [NC]
RewriteRule ^admin/?$ https://my.website/admin/ [L,R=301]
</IfModule>
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# END WithoutWWW
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
How can i make this .htaccess stop looping ??
Use instead:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.my\.website$ [NC]
RewriteRule ^admin/?$ https://my.website/admin/ [L,R=301]
</IfModule>

Redirect Page in Subfolder To New Folder

All of these redirects work except the pages located in sub directories for example:
fr/stuff
to
fr/other-stuff
does not work
RewriteEngine On
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/?stuff$ http://example.org/more-stuff [R=301,L]
RewriteRule ^/?fr/stuff$ http://example.org/fr/other-stuff [R=301,L]
RewriteRule ^/?es/notice\.php$ http://example.org/es/ [R=301,L,QSD]
RewriteRule ^/?len.php$ http://example.org/es/ [R=301,L,QSD]
this is the rest of the code
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
RewriteEngine On
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \.htm
RewriteRule ^(.*)\.htm$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.htm -f
RewriteRule ^([A-Za-z\-]+)$ $1.htm
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^\.localhost$ [NC]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
<FILES .htaccess>
order allow,deny
deny from all
</FILES>
/fr/stuff will redirect correctly to /fr/stuff
but /fr/stuff/ and other content in the directory will not redirect.
To redirect /fr/stuff/ you should do like this
RewriteRule ^/?fr/stuff/?$ http://example.org/fr/other-stuff [R=301,L]
To redirect the subdirectory do like this
RewriteRule ^/?fr/stuff/?(.*)$ http://example.org/fr/other-stuff/$1 [R=301,L]
You should read more about regular expression for details.

.htaccess redirect example.com/magento/index.php to example.com/?

I have Magento installed in a subdirectory, the path to Magento is example.com/magento.
I have an .htaccess file in the root, 'example.com' as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^magento/(.+)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/magento(/.*)?$ [NC]
RewriteRule ^(.*)$ /magento/$1 [QSA,L]
In the standard Magento .htaccess in example.com/magento I have uncommented the RewriteBase directive as follows:
RewriteBase /magento/
But when I type example.com the browser shows example.com/magento
How could I solve this issue?
Leave RewriteBase commented
# RewriteBase /magento/
Try this, instead, in your /.htacccess:
RewriteEngine on
RewriteRule ^(.*)$ /magento/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
and add this to your magento/.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L]
</IfModule>

subdomains with only .htaccess

i got url link region ex. http://mysite.it/index.php?page=search&sRegion=Toscana
is possible make with htaccess an url like toscana.mysite.it
Thank for answers
This is my htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.it$ [NC]
RewriteCond %{QUERY_STRING} (^|&)sRegion=([^&]*) [NC]
RewriteRule ^index\.php$ http://%2.%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.it$ [NC]
RewriteCond %{QUERY_STRING} (^|&)sRegion=([^&]+) [NC]
RewriteRule ^index\.php$ http://%2.%{HTTP_HOST}%{REQUEST_URI} [R=302,NC,L]

Wrong 301 htaccess redirection

I have this code on my site and I need to redirect the http://softsolutions.fr to http://www.softsolutions.fr, but it is not redirecting:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options +Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^softsolutions\.fr
RewriteRule ^(.*)$ http://www.softsolutions.fr/index.html [R=301,L]
RewriteRule ^$ /index.html [L]
# Rewrites "sub.domain.foo/anything" to "sub.domain.foo/anything.php"
RewriteCond %{REQUEST_FILENAME} !^(.+).php$
RewriteCond %{REQUEST_FILENAME} !^(.+).pdf$
RewriteCond %{REQUEST_FILENAME} !^(.+).(html|htm)$
RewriteRule ^([a-zA-Z0-9\-\_/]*)$ /$1.php [L]
</IfModule>
Replace those lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^softsolutions\.fr
RewriteRule ^(.*)$ http://www.softsolutions.fr/index.html [R=301,L]
With:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^softsolutions\.fr [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI}

Resources