Understanding .htaccess RewriteRule with examples

Asked 2 years ago 323 views 1 answer Modified 10 hours ago
18
I'm trying to understand how .htaccess RewriteRule works for URL rewriting and redirection. Can someone explain the syntax and provide practical examples of common RewriteRule patterns? I'd like to see examples for: - Clean URLs - Redirects - Parameter handling - HTTPS enforcement
shama-naaz
50 reputation

1 Answer

0
Yeh kuch examples hain .htaccess rewrite rules ke: ## Example 1: URL ko clean banane ke liye ```apache RewriteEngine On RewriteRule ^([^/]+)/([^/]+)/?$ index.php?category=$1&subcategory=$2 [L,QSA] ``` Yeh rule /category/subcategory/ ko /index.php?category=category&subcategory=subcategory mein badal dega. ## Example 2: URL ko redirect karne ke liye ```apache RewriteEngine On RewriteRule ^old-page/?$ new-page [R=301,L] ``` Yeh rule /old-page/ ko /new-page/ mein permanently redirect karega (301 status code). ## Example 3: URL ko trailing slash se remove karne ke liye ```apache RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.+)/$ $1 [R=301,L] ``` Yeh rule trailing slash ko remove karega, jaise /page/ ko /page mein badal dega. ## Example 4: URL ko parameter se remove karne ke liye ```apache RewriteEngine On RewriteRule ^([^/]+)/(?:id/)?([0-9]+)/?$ $1/$2 [R=301,L] ``` Yeh rule /category/id/123/ ko /category/123/ mein badal dega. ## Example 5: URL ko HTTPS mein redirect karne ke liye ```apache RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] ``` Yeh rule HTTP request ko HTTPS mein permanently redirect karega. aur jankari ke liye related post dekhen
imran-nadwi
answered 1 years ago
You can use Markdown to format your answer.
Last edited: 1 years ago
Preview: